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:
- Either something similar to
REMAINDER
that causes argparse to collect all strings up to the next known argument - an option that tells argparse to ignore dashes in unknown arguments when using
nargs='+'
.