Argparse:在“可选参数"下列出的必需参数? [英] Argparse: Required arguments listed under "optional arguments"?

查看:100
本文介绍了Argparse:在“可选参数"下列出的必需参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下简单代码来解析一些参数;请注意,其中之一是必需的.不幸的是,当用户在不提供参数的情况下运行脚本时,显示的用法/帮助文本并不表示存在一个非可选的参数,我觉得这很令人困惑.我如何获得python来指示参数不是可选的?

I use the following simple code to parse some arguments; note that one of them is required. Unfortunately, when the user runs the script without providing the argument, the displayed usage/help text does not indicate that there is a non-optional argument, which I find very confusing. How can I get python to indicate that an argument is not optional?

这是代码:

import argparse
if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Foo')
    parser.add_argument('-i','--input', help='Input file name', required=True)
    parser.add_argument('-o','--output', help='Output file name', default="stdout")
    args = parser.parse_args()
    print ("Input file: %s" % args.input )
    print ("Output file: %s" % args.output )

在上面的代码中运行而未提供必需的参数时,我得到以下输出:

When running above code without providing the required argument, I get the following output:

usage: foo.py [-h] -i INPUT [-o OUTPUT]

Foo

optional arguments:
    -h, --help            show this help message and exit
    -i INPUT, --input INPUT
                          Input file name
    -o OUTPUT, --output OUTPUT
                          Output file name

推荐答案

---开头的参数通常被认为是可选的.所有其他参数都是位置参数,因此是设计所必需的(例如位置函数自变量).可能需要可选参数,但这有点违背它们的设计.由于它们仍然是非位置参数的一部分,因此即使需要它们,它们仍将列在令人困惑的标题可选参数"下.但是,用法部分缺少的方括号表明确实需要使用方括号.

Parameters starting with - or -- are usually considered optional. All other parameters are positional parameters and as such required by design (like positional function arguments). It is possible to require optional arguments, but this is a bit against their design. Since they are still part of the non-positional arguments, they will still be listed under the confusing header "optional arguments" even if they are required. The missing square brackets in the usage part however show that they are indeed required.

另请参见文档:

通常,argparse模块假定-f和--bar之类的标志表示可选参数,可以在命令行中始终忽略它们.

In general, the argparse module assumes that flags like -f and --bar indicate optional arguments, which can always be omitted at the command line.

注意:通常,必需选项被认为是不好的形式,因为用户希望这些选项是可选的,因此应尽可能避免使用它们.

Note: Required options are generally considered bad form because users expect options to be optional, and thus they should be avoided when possible.

话虽如此,帮助中的标头位置参数" 可选参数" 由两个参数组生成,其中两个参数自动分离.现在,您可以破解"并更改可选参数的名称,但是更优雅的解决方案是为必需的命名参数"(或您要调用的任何参数)创建另一个组:

That being said, the headers "positional arguments" and "optional arguments" in the help are generated by two argument groups in which the arguments are automatically separated into. Now, you could "hack into it" and change the name of the optional ones, but a far more elegant solution would be to create another group for "required named arguments" (or whatever you want to call them):

parser = argparse.ArgumentParser(description='Foo')
parser.add_argument('-o', '--output', help='Output file name', default='stdout')
requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument('-i', '--input', help='Input file name', required=True)
parser.parse_args(['-h'])

usage: [-h] [-o OUTPUT] -i INPUT

Foo

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Output file name

required named arguments:
  -i INPUT, --input INPUT
                        Input file name

这篇关于Argparse:在“可选参数"下列出的必需参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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