arg/README.md
2019-02-14 19:43:39 -08:00

26 lines
1.2 KiB
Markdown

# 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 much too
complicated. It's much nicer to be able to write shell scripts like this:
```
#!/bin/sh
dir=`arg --dir "$@" || arg -d "$@" || echo "$HOME/Downloads"`
if arg --verbose "$@" || arg -v "$@"; then
echo "Downloading to $dir"
fi
cd $path && curl -O example.com/file.zip
```
## Usage
The following forms are accepted: `arg -f ...`, `arg -flag ...`, or `arg --flag
...`. If `arg` finds the first given flag among the rest of the command line
arguments, it will print the flag's value (if any) and exit successfully,
otherwise, it will fail (exit status 1). The value may be of the form
`--flag=value`, `--flag value`, `-f value`, or `-f=value`. Single-letter flags
will match when grouped with other single letter flags, but will not have a
value, i.e. `arg -b -abc foo` will exit successfully 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 parse properly, like
`my_script.sh --flag "one two"`.