I recommend to use pipx to install argparser.
pipx install git+https://github.com/de9uch1/argparser.git
Also you can install by pip.
pip install git+https://github.com/de9uch1/argparser.git
- Define add_args() function and add arguments.
- The first argument of
argparser add
is a variable name in the script which will be set to the given argument value. - The second positional argument, --long/-l and --short/-s options will be command line argument names.
- Other arguments of
argparser add
can be shown byargparser add --help
. eval $(add_args | argparser parse "$@")
parses command line arguments.
The parser is based on argparse.ArgumentParser from Python.
#!/bin/bash
function add_args() {
argparser setup $0 "Test script."
argparser add FILE file
argparser add WORKERS -l num-workers -s n --type int --default 8
argparser add USER_IDS -l user-ids -s u --type int --nargs "*"
argparser add BETA -l experimental --action store_true
argparser add LANGUAGE -l lang --choices en de ja
}
eval $(add_args | argparser parse "$@")
echo $FILE
echo $WORKERS
echo ${USER_IDS[@]}
$BETA && echo T || echo F
echo $LANGUAGE
$ ./script.sh log.txt --num-workers 16 -u 100 200 --experimental --lang ja
The variables in the script will be set to:
FILE=log.txt
WORKERS=16
USER_IDS=(100 200)
# Stored in arrayBETA=true
# This is helpful for such situation:if $BETA; then ...
LANGUAGE=ja
You can also see the help messages of the defined arguments by ./script.sh --help
.
This software was inspired by https://github.com/ko1nksm/getoptions .