1

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='+'.
zeawoas
  • 446
  • 7
  • 18
  • It is not possible to make the arguments of bash scripts be strings? ```python python script.py \ --bash-args1 '--param1 val1 --param2 --val2' \ --bash-args2 'bla --param3 val3 --blu' ``` – Brinfer Apr 19 '22 at 11:19
  • This is what I'm doing so far, but then the user needs to quote the arguments and I need to split the strings before passing them to `subprocess.run`. It works, but it's not as clean as I'd like it to be – zeawoas Apr 19 '22 at 11:24
  • that's a poor fit for `argparse`. – hpaulj Apr 19 '22 at 11:32
  • @hpaulj, why? It would only need an extra option to ignore the prefix char in unrecognized arguments and this would give argparse a lot more flexibility for using it in wrapper scripts. – zeawoas Apr 19 '22 at 11:37
  • This [answer](https://stackoverflow.com/a/12818237/18419414) could help you, just get the unknown arguments and make each `--bash-args1` a `subparser` – Brinfer Apr 19 '22 at 11:37
  • @Brinfer, how would you implement such behaviour with subparsers? AFAIK they are invoked based on the value of the first positional argument. How can one use multiple subparsers for one list of arguments? – zeawoas Apr 19 '22 at 11:48
  • Error on my part, you can not call several subcommands at the same time, it would not be the best solution. – Brinfer Apr 19 '22 at 12:19
  • 1
    `argparse` does not have an option to ignore unrecognized flag strings. Such behavior has been discussed extensively on the bug/issues tracker and closed, https://bugs.python.org/issue9334 – hpaulj Apr 19 '22 at 14:55
  • That's a shame, but thanks for the added context, @hpaulj! – zeawoas Apr 19 '22 at 15:25

1 Answers1

0

For posterity:

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.

zeawoas
  • 446
  • 7
  • 18