code / arg

Lines98 C35 Markdown34 make29
(40 lines)

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:

#!/bin/sh
dir=`arg --dir "$@" || arg -d "$@" || echo "$HOME/Downloads"`
if ! server=`arg --server "$@" || arg -s "$@"`; then
    echo "No server provided :(" && exit 1
fi
if arg --verbose "$@" || arg -v "$@"; then
    echo "Downloading to $dir"
fi
curl "$server/file.zip" > "$dir/file.zip"

The flag's value may be of the form <flag>=<value> or <flag> <value>. Single-letter flags will also match when grouped with other single letter flags, but will not have a value (e.g. arg -b -abc foo will succeed without printing anything).

When using arg in a shell script, it is best to use quotes around $@, as in arg --foo "$@", so arguments with spaces will be forwarded correctly.

1 # arg - A simple command line argument parser
2 This is a simple tool with a simple job: figure out the value of a single
3 command line argument. Tools like `getopt` and `getopts` are unnecessarily
4 complex and difficult to use, while also not supporting common use cases well.
5 Parsing an argument in a shell script shouldn't require a tutorial or dozen
6 lines of shell code!
8 To quote the `getopt` manpage:
10 > Each shellscript has to carry complex code to parse arguments halfway
11 > correcty (like the example presented here). A better getopt-like tool would
12 > move much of the complexity into the tool and keep the client shell scripts
13 > simpler.
15 ## Usage
16 Simply run: `arg <flag> <extra args...>`. `<flag>` can be anything, like `-f`,
17 `-flag`, `--flag`, or `flag`. If the flag occurs among the rest of the
18 command line arguments, `arg` will print the flag's value (if any) and exit
19 successfully, otherwise, it will fail (exit status 1). In a shell script, this
20 would look like:
22 ```
23 #!/bin/sh
24 dir=`arg --dir "$@" || arg -d "$@" || echo "$HOME/Downloads"`
25 if ! server=`arg --server "$@" || arg -s "$@"`; then
26 echo "No server provided :(" && exit 1
27 fi
28 if arg --verbose "$@" || arg -v "$@"; then
29 echo "Downloading to $dir"
30 fi
31 curl "$server/file.zip" > "$dir/file.zip"
32 ```
34 The flag's value may be of the form `<flag>=<value>` or `<flag> <value>`.
35 Single-letter flags will also match when grouped with other single letter
36 flags, but will not have a value (e.g. `arg -b -abc foo` will succeed without
37 printing anything).
39 When using `arg` in a shell script, it is best to use quotes around `$@`, as in
40 `arg --foo "$@"`, so arguments with spaces will be forwarded correctly.