Skip to content

Commit

Permalink
Fix option parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
zwets committed Jun 21, 2017
1 parent da99203 commit f6cd15c
Show file tree
Hide file tree
Showing 15 changed files with 469 additions and 454 deletions.
1 change: 1 addition & 0 deletions uf
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ else
END { print "\n" }' "$@"
fi

# vim: sts=4:sw=4:et:si:ai
43 changes: 22 additions & 21 deletions uf-bare
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

# Function to exit this script with an error message on stderr
err_exit() {
echo "$(basename "$0"): $*" >&2
exit 1
echo "$(basename "$0"): $*" >&2
exit 1
}

# Function to show usage information and exit
usage_exit() {
echo "
echo "
Usage: $(basename $0) [OPTIONS] [FILE ...]
Output only the bare sequences from each FILE to standard output, dropping the
Expand All @@ -39,29 +39,29 @@ Usage: $(basename $0) [OPTIONS] [FILE ...]
By default headers go to /dev/null. Use option -w to store them in a file.
The file can be merged back in with bare sequences using 'uf-dress'.
" >&2
exit ${1:-1}
exit ${1:-1}
}

# Parse options

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
;;
esac
shift
case $1 in
-w) shift
[ $# -ge 1 ] || usage_exit
HDRSFILE="$1"
;;
-f|--force)
FORCE="yes"
;;
--help)
usage_exit 0
;;
*) usage_exit
;;
esac
shift
done

# Check options validity
Expand All @@ -71,6 +71,7 @@ done
# Do the work

awk -b -O -v F="$HDRSFILE" '
NR%2==1 && F { print > F }
NR%2==0' "$@"
NR%2==1 && F { print > F }
NR%2==0' "$@"

# vim: sts=4:sw=4:et:si:ai
47 changes: 24 additions & 23 deletions uf-circut
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

# Function to exit this script with an error message on stderr
err_exit() {
echo "$(basename "$0"): $*" >&2
exit 1
echo "$(basename "$0"): $*" >&2
exit 1
}

# Function to show usage information and exit
usage_exit() {
echo "
echo "
Usage: $(basename $0) [OPTIONS] FROM:END [FILE ...]
$(basename $0) [OPTIONS] FROM/LEN [FILE ...]
$(basename $0) [OPTIONS] MID~DIST [FILE ...]
Expand Down Expand Up @@ -72,7 +72,7 @@ Usage: $(basename $0) [OPTIONS] FROM:END [FILE ...]
- $(basename "$0") -10/21 Same result as above
- $(basename "$0") 100:99 rotates the sequence left by 99 positions
" >&2
exit ${1:-1}
exit ${1:-1}
}

# Parse options
Expand All @@ -81,24 +81,24 @@ QUIET=0
ZERO=0
MARK=0

while [ $# -ne 0 -a "$(expr "$1" : '\(.[0-9]*\)..*')" = "-" ]; do
case $1 in
-m|--mark)
MARK=1
;;
-z|--zero)
ZERO=1
;;
-q|--quiet)
QUIET=1
;;
-h|--help)
usage_exit 0
;;
*) usage_exit
;;
esac
shift
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
;;
esac
shift
done

# Parse the cut specification - arrr walk the regex plank matey
Expand All @@ -117,7 +117,7 @@ RHS="$(expr "$CUT_SPEC" : '.*[/:~]\(.*\)')"
# Delegate to awk

awk -b -O -v P="$(basename "$0")" -v FROM=$LHS -v MID=$LHS -v UPTO=$RHS -v LEN=$RHS -v DIST=$RHS -v OP="$OP" -v Z=$ZERO -v Q=$QUIET -v M=$MARK '
function bump0(p1,p2) { return p1 < 0 && p2 >= 0 ? 1 : 0 } # return 1 if from p1 to p2 crosses 0
function bump0(p1,p2) { return p1 < 0 && p2 >= 0 ? 1 : 0 } # return 1 if from p1 to p2 crosses 0
BEGIN {
# Translate the three possible cut specs into POS0 (left) and POS1 (right)
Expand Down Expand Up @@ -179,3 +179,4 @@ function validate() {
}
' "$@"

# vim: sts=4:sw=4:et:si:ai
51 changes: 26 additions & 25 deletions uf-cut
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

# Function to exit this script with an error message on stderr
err_exit() {
echo "$(basename "$0"): $*" >&2
exit 1
echo "$(basename "$0"): $*" >&2
exit 1
}

# Function to show usage information and exit
usage_exit() {
echo "
echo "
Usage: $(basename $0) [OPTIONS] FROM:END [FILE ...]
$(basename $0) [OPTIONS] FROM/LEN [FILE ...]
$(basename $0) [OPTIONS] MID~DIST [FILE ...]
Expand Down Expand Up @@ -69,7 +69,7 @@ Usage: $(basename $0) [OPTIONS] FROM:END [FILE ...]
- '$(basename "$0") x/1' Same result as previous
- '$(basename "$0") x~0' Same result as previous
" >&2
exit ${1:-1}
exit ${1:-1}
}

# Parse options
Expand All @@ -79,27 +79,27 @@ ZERO=0
CLIP=0
MARK=0

while [ $# -ne 0 -a "$(expr "$1" : '\(.[0-9]*\)..*')" = "-" ]; 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
;;
esac
shift
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
;;
esac
shift
done

# Parse the cut specification - arrr walk the regex plank matey
Expand Down Expand Up @@ -200,3 +200,4 @@ function validate_and_map() {
}
' "$@"

# vim: sts=4:sw=4:et:si:ai
47 changes: 24 additions & 23 deletions uf-dress
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

# Function to exit this script with an error message on stderr
err_exit() {
echo "$(basename "$0"): $*" >&2
exit 1
echo "$(basename "$0"): $*" >&2
exit 1
}

# Function to show usage information and exit
usage_exit() {
echo "
echo "
Usage: $(basename $0) [OPTIONS] [FILE ...]
Insert headers to turn bare sequences into valid unfasta. Reads each line from
Expand All @@ -45,26 +45,26 @@ Usage: $(basename $0) [OPTIONS] [FILE ...]
See also: 'uf-map' which implements this idiom in a single command.
" >&2
exit ${1:-1}
exit ${1:-1}
}

# Parse options

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
;;
esac
shift
case $1 in
-r) shift
[ $# -ge 1 ] || usage_exit
HDRSFILE="$1"
;;
--help)
usage_exit 0
;;
*) usage_exit
;;
esac
shift
done

# Check options validity
Expand All @@ -74,11 +74,12 @@ done
# Do the work

awk -b -O -v F="$HDRSFILE" '{
HDR = ""
if (F) getline HDR < F
if (!HDR) HDR = ">lcl|" NR " Dummy header " NR
print HDR
print
}
END { if (F) close (F) }' "$@"
HDR = ""
if (F) getline HDR < F
if (!HDR) HDR = ">lcl|" NR " Dummy header " NR
print HDR
print
}
END { if (F) close (F) }' "$@"

# vim: sts=4:sw=4:et:si:ai
43 changes: 22 additions & 21 deletions uf-drop
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

# Function to exit this script with an error message on stderr
err_exit() {
echo "$(basename "$0"): $*" >&2
exit 1
echo "$(basename "$0"): $*" >&2
exit 1
}

# Function to show usage information and exit
usage_exit() {
echo "
echo "
Usage: $(basename $0) [OPTIONS] NUM [FILE ...]
For each sequence in each unfasta format FILE drop the first NUM characters
Expand All @@ -38,7 +38,7 @@ Usage: $(basename $0) [OPTIONS] NUM [FILE ...]
-b|--but Drop everything BUT the final NUM characters from each sequence.
-m|--mark Document the edit by appending '(uf:drop:[but:]NUM)' to headers.
" >&2
exit ${1:-1}
exit ${1:-1}
}

# Parse options
Expand All @@ -47,20 +47,20 @@ BUT=0
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
;;
esac
shift
case $1 in
-m|--mark)
MARK=1
;;
-b|--but)
BUT=1
;;
-h|--help)
usage_exit 0
;;
*) usage_exit
;;
esac
shift
done

# Check first argument is positive number
Expand All @@ -73,7 +73,8 @@ shift
# Delegate to awk

awk -b -O -v P="$(basename "$0")" -v N=$NUM -v M=$MARK -v B=$BUT '
NR%2==1 { print $0 (M ? " (uf:drop:" (B?"but:":"") N ")" : "") }
NR%2==0 { print B ? substr ($0,length($0)-N+1) : substr ($0,N+1) }
' "$@"
NR%2==1 { print $0 (M ? " (uf:drop:" (B?"but:":"") N ")" : "") }
NR%2==0 { print B ? substr ($0,length($0)-N+1) : substr ($0,N+1) }
' "$@"

# vim: sts=4:sw=4:et:si:ai
Loading

0 comments on commit f6cd15c

Please sign in to comment.