I have a Python script that will later call multiple Bash scripts with supprocess.run
. When calling the Python script, the user should be able to specify lists of arguments (some of which might start with hyphens) for the Bash scripts like
python script.py \
--bash-args1 --param1 val1 --param2 val2 \
--bash-args2 bla --param3 val3 --blu
Argparse should parse this into Namespace(bash_args1=['--param1', 'val1', '--param2', 'val2'], bash_args2=['bla', '--param3', 'val3', '--blu'])
. Is there a canonical way of achieving this? I cannot use nargs=argparse.REMAINDER
or parser.parse_known_args
because I need to collect the arguments for more than one Bash script and a simple nargs='+'
will fail if the secondary arguments start with dashes.
I guess I would need one of the following:
REMAINDER
that causes argparse to collect all strings up to the next known argumentnargs='+'
.There are a few ways of working around this limitation of argparse
. I wrapped one up and published it on PyPI. You can use it just like argparse
. The only difference is that there is an extra option you can supply as nargs
parameter in add_argument
. When this option is used, the parser collects all unknown arguments (regardless of whether they start with a hyphen or not) until the next known argument. For more info check out the repo on github.