(60 lines)
1 #!/bin/sh2 # Simple shell script to parse one command line argument at a time.3 #4 # Usage: arg <flag> args...5 #6 # If the flag is present among the given arguments, its value (if any) will be printed.7 # Otherwise, the command will fail (subject to the special values below).8 # Positional arguments are those that either don't begin with a '-', or appear9 # after a lone "--" argument.10 #11 # Special Values:12 # @ Print all positional arguments (i.e. excluding flags).13 # <num> Print the Nth positional arg, if present, otherwise fail.14 # <flag>? Pipe output to /dev/null, but still use success status15 # to indicate if flag is present.16 # <flag>=<default> If flag is not present, use <default> as the value.17 # Always succeeds.18 # <flag1>|<flag2> If flag1 is not present, check flag2 instead.19 #20 # Examples:21 # arg '-v|--verbose?' "$@" && verbose=yes || verbose=no22 # if logfile="$(arg --log "$@")"; then ...; fi23 # path="$(arg '-p|--path=/tmp/default' "$@")"24 # for f in $(arg @ "$@"); do ...; done25 # case "$(arg 1 "$@")" in ... esac26 # arg verboten.txt'?' "$@" && echo "You can't use that file!" && exit 127 #28 # Note: space-separated flag/value pairs are not supported, e.g. `ls -I \*.txt`29 # or `ls --ignore \*.txt`. In these cases, please use the following call30 # syntax instead: `ls -I\*.txt` or `ls -I=\*.txt` and `ls --ignore=\*.txt`.31 # Supporting space-separated flag/value pairs would make positional argument32 # functionality impossible because there would be no way to know whether x or y33 # is the first positional arg in these examples: `arg 1 -f x y`, `arg 1 --foo x y`34 # (At least, not without telling `arg` whether or not each flag expects a value)35 #36 # Note 2: If you expect arguments to contain "\x1E", you may want to run `arg`37 # with a custom record separator in the environment variable `RS`. Or, if you want38 # to customize the output of `arg @ ...` to use a custom output record separator,39 # you can use the environment variable `ORS`.40 #54 /^--$/ {exit 0}57 /^--$/ {exit 0}60 esac