0

I have an argument parser with subcommands, however some subcommand parameters are shared across all subcommands (i.e. number of threads to use). Currently, I have pass the shared param like this:

python program.py --threads 10 subcommand --flag1 --flag2

but That seems unnatural and I'd like to support something like

python program.py subcommand --flag1 --flag2 --threads 10

The C++ CLI11 parsing library supports this with "fallthrough" options, where a flag that doesn't match to the subcommand falls through to the main command. Is there any way to do this with argparse other than duplicating the flag for every subcommand?

Throckmorton
  • 564
  • 4
  • 17
  • `argparse` doesn't have that 'fallthrough'. Once parsing is passed to the `subcommand` parser, the main does not do any more. Anything `unknown` to the subparser is unknown to the main. So if you don't want `--threads` in the main, put it in all subs (that's easy to do programmatically). – hpaulj Jul 24 '20 at 00:53
  • A relatively recent question like this: https://stackoverflow.com/questions/62904585/support-global-arguments-before-or-after-sub-command-in-argparse – hpaulj Aug 13 '20 at 06:25

0 Answers0