python - mutually_exclusive_group with optional and positional argument -


i created cli specification docopt works great, reason have rewrite argparse

usage:     update_store_products <store_name>...     update_store_products --all      options:       -a --all     updates stores configured in config 

how that?

what important don't want have this:

update_store_products [--all] <store_name>... 

i think rather this:

update_store_products (--all | <store_name>...) 

i tried use add_mutually_exclusive_group, got error:

valueerror: mutually exclusive arguments must optional 

first off, should include the shortest code necessary reproduce error in question itself. without answer shot in dark.

now, i'm willing bet argparse definitions bit this:

parser = argumentparser() group = parser.add_mutually_exclusive_group(required=true) group.add_argument('--all', action='store_true') group.add_argument('store_name', nargs='*') 

the arguments in mutually exclusive group must optional, because not make sense have required argument there, group have argument ever. nargs='*' alone not enough – required attribute of created action true – convince mutex group argument optional. have add default:

parser = argumentparser() group = parser.add_mutually_exclusive_group(required=true) group.add_argument('--all', action='store_true') group.add_argument('store_name', nargs='*', default=[]) 

this result in:

[~]% python2 arg.py usage: arg.py [-h] (--all | store_name [store_name ...]) arg.py: error: 1 of arguments --all store_name required  [~]% python2 arg.py --all namespace(all=true, store_name=[])  [~]% python2 arg.py store1 store2 store3 namespace(all=false, store_name=['store1', 'store2', 'store3']) 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -