ArgumentParser optional multiple subparsers

33 Views Asked by At

I'm trying to get a configuration for a pipeline script, where my program provides three different actions with their own parameters. I want to be able to selectively run one of the three, or also combine them in a sequence defined by the order of the arguments, for example:

action1
  --p1 (parameter for 1 only)
action2
  --p2 (parameter for 2 only)
--p (global parameter)
...

I would like to be able to do python main.py action1 --p1 or python main.py action2 --p2 but also python main.py action1 --p1 action2 --p2

I thought I would solve this with subparsers with the 'append' action

parser = ArgumentParser()
subparse = parser.add_subparsers(dest="op", action='append')
t = subparse.add_parser("action1")
t.add_argument("--p1", type=int, default=10)
v = subparse.add_parser('action2')
v.add_argument("--p2", type=int, default=1)

but this fails:

  File "C:\Users\andre\example.py", line 231, in <module>
    subparse = parser.add_subparsers(dest="action", action='append')
  File "C:\Users\andre\.pyenv\pyenv-win\versions\3.9.13\lib\argparse.py", line 1798, in add_subparsers
    action = parsers_class(option_strings=[], **kwargs)
TypeError: __init__() got an unexpected keyword argument 'parser_class'

Why is this not working, and which other strategy could I use to solve this?

0

There are 0 best solutions below