arg - A simple command line argument parser
This is a simple tool with a simple job: figure out the value of a single
command line argument. Tools like getopt and getopts are unnecessarily
complex and difficult to use, while also not supporting common use cases well.
Parsing an argument in a shell script shouldn't require a tutorial or dozen
lines of shell code!
To quote the getopt manpage:
Each shellscript has to carry complex code to parse arguments halfway correcty (like the example presented here). A better getopt-like tool would move much of the complexity into the tool and keep the client shell scripts simpler.
Usage
Simply run: arg <flag> <extra args...>. <flag> can be anything, like -f,
-flag, --flag, or flag. If the flag occurs among the rest of the
command line arguments, arg will print the flag's value (if any) and exit
successfully, otherwise, it will fail (exit status 1). In a shell script, this
would look like:
1 #!/bin/sh2 dir=`arg --dir "$@" || arg -d "$@" || echo "$HOME/Downloads"`3 if ! server=`arg --server "$@" || arg -s "$@"`; then4 echo "No server provided :(" && exit 15 fi6 if arg --verbose "$@" || arg -v "$@"; then7 echo "Downloading to $dir"8 fi9 curl "$server/file.zip" > "$dir/file.zip"