mutually_exclusive_group with optional and positional argument

Deadly 提交于 2019-12-02 04:24:12

问题


I created an cli specification with docopt which works great, however for some reason I have to rewrite it to argparse

Usage:
    update_store_products <store_name>...
    update_store_products --all

    Options:
      -a --all     Updates all stores configured in config

How to do that?

What is important I don't want to have something like this:

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

I think it would be rather something like this:

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

I tried to use add_mutually_exclusive_group, but I got error:

ValueError: mutually exclusive arguments must be optional

回答1:


First off, you should include the shortest code necessary to reproduce the error in the question itself. Without it an answer is just a shot in the dark.

Now, I'm willing to bet your argparse definitions look a bit something like 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 a mutually exclusive group must be optional, because it would not make much sense to have a required argument there, as the group could then only have that argument ever. The nargs='*' alone is not enough – the required attribute of the created action will be True – to convince the mutex group that the argument is truly optional. What you have to do is add a 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 will result in:

[~]% python2 arg.py
usage: arg.py [-h] (--all | store_name [store_name ...])
arg.py: error: one of the arguments --all store_name is required

[~]% python2 arg.py --all
Namespace(all=True, store_name=[])

[~]% python2 arg.py store1 store2 store3
Namespace(all=False, store_name=['store1', 'store2', 'store3'])


来源:https://stackoverflow.com/questions/36832893/mutually-exclusive-group-with-optional-and-positional-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!