From ea69d23479eef3380c6946e1199fe2f2dc415e45 Mon Sep 17 00:00:00 2001 From: Marco van Zwetselaar Date: Wed, 27 Jan 2021 01:32:45 +0300 Subject: [PATCH] Gardening the option handling --- uf | 2 +- uf-bare | 18 +++++------------- uf-circut | 21 ++++++--------------- uf-cut | 25 +++++++------------------ uf-dress | 14 ++++---------- uf-drop | 17 +++++------------ uf-freqs | 21 ++++++--------------- uf-hash | 18 +++++------------- uf-headers | 9 +++------ uf-map | 13 ++++--------- uf-random | 39 +++++++++------------------------------ uf-rc | 21 ++++++--------------- uf-select | 12 ++++++------ uf-sort | 13 ++++--------- uf-stats | 6 +++--- uf-take | 17 +++++------------ uf-valid | 39 ++++++++++----------------------------- 17 files changed, 89 insertions(+), 216 deletions(-) diff --git a/uf b/uf index 3330540..bbf878d 100755 --- a/uf +++ b/uf @@ -65,7 +65,7 @@ while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do -h|--help) usage_exit 0 ;; *) usage_exit ;; esac - shift + shift || usage_exit done if [ -n "$REVERT" ]; then diff --git a/uf-bare b/uf-bare index b133f51..8db0aea 100755 --- a/uf-bare +++ b/uf-bare @@ -48,20 +48,12 @@ unset HDRSFILE FORCE while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - -w) shift - [ $# -ge 1 ] || usage_exit - HDRSFILE="$1" - ;; - -f|--force) - FORCE="yes" - ;; - --help) - usage_exit 0 - ;; - *) usage_exit - ;; + -w) shift; HDRSFILE="$1" ;; + -f|--force) FORCE="yes" ;; + --help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Check options validity diff --git a/uf-circut b/uf-circut index a5a6b21..3a4de7a 100755 --- a/uf-circut +++ b/uf-circut @@ -83,22 +83,13 @@ MARK=0 while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - -m|--mark) - MARK=1 - ;; - -z|--zero) - ZERO=1 - ;; - -q|--quiet) - QUIET=1 - ;; - -h|--help) - usage_exit 0 - ;; - *) usage_exit - ;; + -m|--mark) MARK=1 ;; + -z|--zero) ZERO=1 ;; + -q|--quiet) QUIET=1 ;; + -h|--help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Parse the cut specification - arrr walk the regex plank matey diff --git a/uf-cut b/uf-cut index e87c0d6..4eed19d 100755 --- a/uf-cut +++ b/uf-cut @@ -81,25 +81,14 @@ MARK=0 while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - -m|--mark) - MARK=1 - ;; - -c|--clip) - CLIP=1 - ;; - -q|--quiet) - QUIET=1 - ;; - -z|--zero) - ZERO=1 - ;; - -h|--help) - usage_exit 0 - ;; - *) usage_exit - ;; + -m|--mark) MARK=1 ;; + -c|--clip) CLIP=1 ;; + -q|--quiet) QUIET=1 ;; + -z|--zero) ZERO=1 ;; + -h|--help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Parse the cut specification - arrr walk the regex plank matey diff --git a/uf-dress b/uf-dress index d74003a..514f137 100755 --- a/uf-dress +++ b/uf-dress @@ -54,17 +54,11 @@ unset HDRSFILE while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - -r) shift - [ $# -ge 1 ] || usage_exit - HDRSFILE="$1" - ;; - --help) - usage_exit 0 - ;; - *) usage_exit - ;; + -r) shift; HDRSFILE="$1" ;; + --help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Check options validity diff --git a/uf-drop b/uf-drop index 3f86ec3..a64d4d6 100755 --- a/uf-drop +++ b/uf-drop @@ -48,19 +48,12 @@ MARK=0 while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - -m|--mark) - MARK=1 - ;; - -b|--but) - BUT=1 - ;; - -h|--help) - usage_exit 0 - ;; - *) usage_exit - ;; + -m|--mark) MARK=1 ;; + -b|--but) BUT=1 ;; + -h|--help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Check first argument is positive number diff --git a/uf-freqs b/uf-freqs index 7e03adb..bad88d5 100755 --- a/uf-freqs +++ b/uf-freqs @@ -49,22 +49,13 @@ TOTALS=0 while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - -b|--bare) - BARE=1 - ;; - -l|--length*) - LENGTH_ONLY=1 - ;; - -t|--totals) - TOTALS=1 - ;; - -h|--help) - usage_exit 0 - ;; - *) usage_exit - ;; + -b|--bare) BARE=1 ;; + -l|--length*) LENGTH_ONLY=1 ;; + -t|--totals) TOTALS=1 ;; + -h|--help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done gawk -bO -v P="$(basename "$0")" -v BARE=$BARE -v LENGTH_ONLY=$LENGTH_ONLY -v TOTALS=$TOTALS ' diff --git a/uf-hash b/uf-hash index 47f612f..e361823 100755 --- a/uf-hash +++ b/uf-hash @@ -50,20 +50,12 @@ Usage: $(basename $0) [OPTIONS] [FILE ...] unset REORDERABLE while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - --algo=*) - ALGO=${1#--algo=} - ;; - -a|--algo*) - shift - ALGO=$1 - ;; - -h|--help) - usage_exit 0 - ;; - *) usage_exit - ;; + --algo=*) ALGO=${1#--algo=} ;; + -a|--algo*) shift; ALGO=$1 ;; + -h|--help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Check options and arguments diff --git a/uf-headers b/uf-headers index b9235a5..45fc708 100755 --- a/uf-headers +++ b/uf-headers @@ -39,13 +39,10 @@ Usage: $(basename $0) [OPTIONS] [FILE ...] while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - --help) - usage_exit 0 - ;; - *) usage_exit - ;; + --help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done gawk -bO 'NR%2==1' "$@" diff --git a/uf-map b/uf-map index ffe2162..cfce137 100755 --- a/uf-map +++ b/uf-map @@ -54,16 +54,11 @@ Usage: $(basename $0) [OPTIONS] OPERATION [FILE ...] unset TITLES while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - -t|--title*) - TITLES=1 - ;; - -h|--help) - usage_exit 0 - ;; - *) usage_exit - ;; + -t|--title*) TITLES=1 ;; + -h|--help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Parse arguments diff --git a/uf-random b/uf-random index a300f93..7af05ce 100755 --- a/uf-random +++ b/uf-random @@ -53,37 +53,16 @@ Usage: $(basename $0) [OPTIONS] [CHARS] while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - --count=*) - SEQ_COUNT=${1#--count=} - ;; - -c|--count) - shift - [ $# -ge 1 ] || usage_exit - SEQ_COUNT=$1 - ;; - --length=*) - SEQ_LENGTH=${1#--length=} - ;; - -l|--length) - shift - [ $# -ge 1 ] || usage_exit - SEQ_LENGTH=$1 - ;; - --range=*) - RANGE=${1#--range=} - ;; - -r|--range) - shift - [ $# -ge 1 ] || usage_exit - RANGE=$1 - ;; - --help) - usage_exit 0 - ;; - *) usage_exit - ;; + --count=*) SEQ_COUNT=${1#--count=} ;; + -c|--count) shift; SEQ_COUNT=$1 ;; + --length=*) SEQ_LENGTH=${1#--length=} ;; + -l|--length) shift; SEQ_LENGTH=$1 ;; + --range=*) RANGE=${1#--range=} ;; + -r|--range) shift; RANGE=$1 ;; + --help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Parse arguments diff --git a/uf-rc b/uf-rc index 779efdb..a594b23 100755 --- a/uf-rc +++ b/uf-rc @@ -64,22 +64,13 @@ MARK=0 while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - -r|--reverse-only) - COMPLEMENT=0 - ;; - -c|--complement-only) - REVERSE=0 - ;; - -m|--mark*) - MARK=1 - ;; - --help) - usage_exit 0 - ;; - *) usage_exit - ;; + -r|--reverse-only) COMPLEMENT=0 ;; + -c|--complement-only) REVERSE=0 ;; + -m|--mark*) MARK=1 ;; + --help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Do the work diff --git a/uf-select b/uf-select index 89fe3d9..8701f80 100755 --- a/uf-select +++ b/uf-select @@ -51,16 +51,16 @@ unset NTH SEQID REGEX while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in --nth=*) NTH=${1#--nth=} ;; - -n|--nth) shift; [ $# -ge 1 ] || usage_exit; NTH=$1 ;; + -n|--nth) shift; NTH=$1 ;; --id=*) SEQID=${1#--id=} ;; - -i|--id) shift; [ $# -ge 1 ] || usage_exit; SEQID=$1 ;; + -i|--id) shift; SEQID=$1 ;; --grep=*) REGEX=${1#--grep=} ;; - -g|--grep) shift; [ $# -ge 1 ] || usage_exit; REGEX=$1 ;; + -g|--grep) shift; REGEX=$1 ;; -h|--help) usage_exit 0 ;; - -) break ;; - *) usage_exit ;; + -) break ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Handle option-less case diff --git a/uf-sort b/uf-sort index 9f588f0..3a19701 100755 --- a/uf-sort +++ b/uf-sort @@ -45,16 +45,11 @@ unset REVERSE while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - -r|--reverse) - REVERSE="-r" - ;; - -h|--help) - usage_exit 0 - ;; - *) usage_exit - ;; + -r|--reverse) REVERSE="-r" ;; + -h|--help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Do the work diff --git a/uf-stats b/uf-stats index 1060037..4db4da7 100755 --- a/uf-stats +++ b/uf-stats @@ -56,15 +56,15 @@ BARE=0 while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in --min-len*=*) MIN_LEN="${1##--min-len*=}" ;; - -m|--min-len) shift || usage_exit; MIN_LEN="$1" ;; + -m|--min-len) shift; MIN_LEN="$1" ;; --delim=*) DELIM="${1##--delim=}" ;; - -d|--delim) shift || usage_exit; DELIM="$1" ;; + -d|--delim) shift; DELIM="$1" ;; -t|--trans*) TRANS=1 ;; -b|--bare) BARE=1 ;; -h|--help) usage_exit 0 ;; *) usage_exit ;; esac - shift + shift || usage_exit done gawk -bO -v P="$(basename "$0")" -v MIN_LEN="$MIN_LEN" -v DELIM="$DELIM" -v TRANS="$TRANS" -v BARE="$BARE" ' diff --git a/uf-take b/uf-take index 37606ab..029b2f3 100755 --- a/uf-take +++ b/uf-take @@ -47,19 +47,12 @@ MARK=0 while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - -m|--mark) - MARK=1 - ;; - -b|--but) - BUT=1 - ;; - -h|--help) - usage_exit 0 - ;; - *) usage_exit - ;; + -m|--mark) MARK=1 ;; + -b|--but) BUT=1 ;; + -h|--help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Check first argument is positive number diff --git a/uf-valid b/uf-valid index 0b43e99..399f107 100755 --- a/uf-valid +++ b/uf-valid @@ -100,14 +100,6 @@ QUIET=0 while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do case $1 in - --allow=*) - ALLOW="${ALLOW}${1#"--allow="}" - ;; - -a|--allow) - shift - [ $# -ge 1 ] || usage_exit - ALLOW="${ALLOW}$1" - ;; --dna) [ -z "$ALLOW" ] || err_exit "option $1 may be followed by --allow but not otherwise combined" ALLOW="$DNA_ALPHABET" @@ -128,28 +120,17 @@ while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do ALLOW="$AMINO_ALPHABET" IGNORE_CASE=1 ;; - -i|--ignore*) - IGNORE_CASE=1 - ;; - -v|--headers) - VAL_HEADERS=1 - ;; - -s|--stop) - STOP_ON_ERR=1 - ;; - -k|--keep-errors) - KEEP_ERRORS=1 - ;; - -q|--quiet) - QUIET=1 - ;; - --help) - usage_exit 0 - ;; - *) usage_exit - ;; + --allow=*) ALLOW="${ALLOW}${1#"--allow="}" ;; + -a|--allow) shift; ALLOW="${ALLOW}$1" ;; + -i|--ignore*) IGNORE_CASE=1 ;; + -v|--headers) VAL_HEADERS=1 ;; + -s|--stop) STOP_ON_ERR=1 ;; + -k|--keep*) KEEP_ERRORS=1 ;; + -q|--quiet) QUIET=1 ;; + --help) usage_exit 0 ;; + *) usage_exit ;; esac - shift + shift || usage_exit done # Check that at least some alphabet was selected