Python 3 argparse:仅当未提供可选参数时才需要条件参数吗? [英] Python 3 argparse: Conditional argument only required if optional argument not provided?
问题描述
[Python 3/argparse]
[Python 3/argparse]
假设我的应用需要一个位置参数:
Suppose my app expects a positional argument:
myApp.py [desired_function]
但是,如果用户提供了某些可选选项,则不需要 desired_function
.
However, if the user provides a certain optional, then desired_function
should NOT be required.
myApp.py --list-functions
在这种情况下,如果传递了-list-functions
,我希望应用程序继续运行,而将我设置为 desired_function
的变量保留为空,因为代码会看到用户通过了-list-functions
并采取了相应的行动.
In this instance, if --list-functions
is passed, I want the application to proceed, leaving the variable I set desired_function
into to remain empty, because my code will see that the user passed --list-functions
and act accordingly.
但是,如果用户未不提供-list-functions
选项,则参数解析器将产生错误,因为用户未提供所需的位置信息论点.
However, if the user does not provide the --list-functions
option, then the argument parser should produce an error because the user did not provide a required positional argument.
如果用户未提供可选的选项,我如何让argparse将 desired_function
定位为必需的 ?
How can I have argparse make the desired_function
positional required only if the user has not provided the optional?
到目前为止我拥有的代码:
Code I have so far:
parser = argparse.ArgumentParser(description="test app")
parser.add_argument("desired_function", help="The function desired", action="store", default="", dest="func")
parser.add_argument("--list-functions",help="List the available functions", action="store_true", default=False, dest="list_mode")
在这种状态下,调用 myApp.py --list-functions
将失败,并显示以下信息:
In this state, the invocation myApp.py --list-functions
will fail with:
usage: myApp.py [-h] [--list-functions] desired_function
myApp.py: error: the following arguments are required: desired_function
推荐答案
这是一个简单的解析器,使位置参数为可选(nargs =?).
Here's a simple parser that makes the positional argument optional (nargs=?).
In [171]: parser=argparse.ArgumentParser()
In [172]: parser.add_argument('func', nargs='?',default='foo')
In [173]: parser.add_argument('--list',action='store_true')
In [174]: args=parser.parse_args(['--list'])
In [175]: args
Out[175]: Namespace(func='foo', list=True)
In [176]: args=parser.parse_args(['bar'])
In [177]: args
Out[177]: Namespace(func='bar', list=False)
In [178]: parser.parse_args(['--list','bar'])
Out[178]: Namespace(func='bar', list=True)
In [179]: parser.parse_args([])
Out[179]: Namespace(func='foo', list=False)
不要为位置参数提供 dest
参数;它已经在第一个字符串中获得了一个. store_true
操作不需要默认设置;它会自动获取 False
.
Don't give a positional argument a dest
parameter; it already gets one in the first string. store_true
action doesn't need a default; it gets False
automatically.
通过此设置,您可以检查 args.list
;它为 True
,然后忽略 args.func
值.如果 False
,请使用 args.func
.如果为 func
提供了有意义的默认值,则用户是否为您提供值都无关紧要.换句话说,它实际上不是必需的.(如果需要的话,定义默认值是没有意义的).或者,如果他们俩都给您,您可能会反对:
With this set up, you could check args.list
; it True
, then just ignore the args.func
value. If False
, use the args.func
. If you have given func
a meaningful default, if shouldn't matter whether the user gives you a value or not. In other words, it really isn't required. (And if required there's no point in defining a default). Or you could object if they give you both:
In [184]: args=parser.parse_args(['--list','bar'])
In [185]: if args.list and args.func!='foo':
parser.error('you stupid user ...')
.....:
usage: ipython3 [-h] [--list] [func]
ipython3: error: you stupid user ...
我们可以进一步将这两个参数放在 mutually_exclusive_group
中,并使该组成为 required
.这样的组接受这样的一个可选位置.我认为文档足够清晰,但是如果需要,我可以扩大答案.
We could go the further step of placing these 2 arguments in a mutually_exclusive_group
, and making that group required
. Such a group accepts one optional positional like this. I think the docs are clear enough, but I could expand my answer if needed.
这篇关于Python 3 argparse:仅当未提供可选参数时才需要条件参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!