Python 3 argparse:仅当未提供可选参数时才需要条件参数? [英] Python 3 argparse: Conditional argument only required if optional argument not provided?

查看:18
本文介绍了Python 3 argparse:仅当未提供可选参数时才需要条件参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[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?

我到目前为止的代码:

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 ...

我们可以更进一步,将这 2 个参数放在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆