bash-argparse
is a tool to parse your bash
scripts command-line arguments.
- Specify command-line arguments from a signature
- Short and long options
- Positional and optional arguments
- Type checking
- Default values
- Only requires a standard python (>= 3.7) installation
- Ease-of-use: If you already know any typed programming language,
bash-argparse
may feel more intuitive, since the script signature resembles a normal function definition. - Type Safety: Skip writing input validation by relying on
bash-argparse
checking the user's input against the type specification. - Automatic: Rely on
bash-argparse
deducing the program's name and short-options.
- Python 3.7 or above
Install it from pip
:
pip install bash-argparse
Or check the artifacts from the GitHub actions pipelines to download a nightly build.
Here is a basic example:
#!/bin/bash
set -eou pipefail
# accepts 4 options passed to the script (through `$@`)
# * a boolean flag --foo/-f, or --no-foo
# * an integer option --bar <I>/-b<I>
# * and a string option --fuz <S>/-F <S>
# * --help or -h to print the automatic help message
eval $( python3 -m bash-argparse \
--signature "bool foo; int bar; string Fuz" \
--description "This program does many things." \
-- "$@" )
# the variables are set by `eval`
echo $FOO $BAR $FUZ
bash program.sh
printsfalse 0
bash program.sh --bar 4
printsfalse 4
bash program.sh -f --bar 4
printstrue 4
bash program.sh -f --fuz 'hello'
printstrue 0 hello
See the documentation in the doc/ directory
This project is distributed under the MIT License. For more information see the LICENSE file.