如何使用 Python Argparse 制作所需参数的短版和长版? [英] How to make a short and long version of a required argument using Python Argparse?
问题描述
我想指定一个名为 inputdir
的必需参数,但我也想要一个名为 i
的速记版本.我没有看到一个简洁的解决方案来做到这一点,而无需同时设置两个可选参数,然后进行我自己的检查.是否有我没有看到的首选做法,或者唯一的方法是将两者都设为可选并自行处理错误?
这是我的代码:
导入 argparse解析器 = argparse.ArgumentParser()parser.add_argument("inputdir", help="指定输入目录")parser.parse_args()
For flags(选项以 -
或 --
开头)pass在选项中 with 标志.您可以指定多个选项:
parser.add_argument('-i', '--inputdir', help="指定输入目录")
查看名称或标志选项文档:
<块引用>add_argument()
方法必须知道是可选参数,如 -f
或 --foo
,还是位置参数,如一个文件名列表,是预期的.因此,传递给 add_argument()
的第一个参数必须是一系列标志或简单的参数名称.
演示:
<预><代码>>>>导入参数解析>>>解析器 = argparse.ArgumentParser()>>>parser.add_argument('-i', '--inputdir', help="指定输入目录")_StoreAction(option_strings=['-i', '--inputdir'], dest='inputdir', nargs=None, const=None, default=None, type=None, choice=None, help='指定输入目录', 元变量 = 无)>>>parser.print_help()用法:[-h] [-i INPUTDIR]可选参数:-h, --help 显示此帮助信息并退出-i INPUTDIR, --inputdir INPUTDIR指定输入目录>>>parser.parse_args(['-i', '/some/dir'])命名空间(inputdir='/some/dir')>>>parser.parse_args(['--inputdir', '/some/dir'])命名空间(inputdir='/some/dir')然而,必需 参数的第一个元素只是一个占位符.-
和 --
选项总是可选的(这是命令行约定),必需的参数永远不会用这样的开关指定.相反,命令行帮助将根据传递给 add_argument()
的第一个参数使用占位符显示放置所需参数的位置,该参数将不带破折号传入.
如果您必须打破该约定并使用以 -
或 --
开头的参数,无论如何,您将必须自己检查:
args = parser.parse_args()如果不是 args.inputdir:parser.error('请用 -i 或 --inputdir 选项指定一个输入目录')
这里是 parser.error()
方法 将打印帮助信息和错误信息,然后退出.
I want to specify a required argument called inputdir
but I also would like to have a shorthand version of it called i
. I don't see a concise solution to do this without making both optional arguments and then doing my own check. Is there a preferred practice for this that I'm not seeing or the only way is to make both optional and do my own error-handling?
Here is my code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("inputdir", help="Specify the input directory")
parser.parse_args()
For flags (options starting with -
or --
) pass in options with the flags. You can specify multiple options:
parser.add_argument('-i', '--inputdir', help="Specify the input directory")
See the name or flags option documentation:
The
add_argument()
method must know whether an optional argument, like-f
or--foo
, or a positional argument, like a list of filenames, is expected. The first arguments passed toadd_argument()
must therefore be either a series of flags, or a simple argument name.
Demo:
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-i', '--inputdir', help="Specify the input directory")
_StoreAction(option_strings=['-i', '--inputdir'], dest='inputdir', nargs=None, const=None, default=None, type=None, choices=None, help='Specify the input directory', metavar=None)
>>> parser.print_help()
usage: [-h] [-i INPUTDIR]
optional arguments:
-h, --help show this help message and exit
-i INPUTDIR, --inputdir INPUTDIR
Specify the input directory
>>> parser.parse_args(['-i', '/some/dir'])
Namespace(inputdir='/some/dir')
>>> parser.parse_args(['--inputdir', '/some/dir'])
Namespace(inputdir='/some/dir')
However, the first element for required arguments is just a placeholder. -
and --
options are always optional (that's the command line convention), required arguments are never specified with such switches. Instead the command-line help will show where to put required arguments with a placeholder based on the first argument passed to add_argument()
, which is to be passed in without dashes.
If you have to break with that convention and use an argument starting with -
or --
that is required anyway, you'll have to do your own checking:
args = parser.parse_args()
if not args.inputdir:
parser.error('Please specify an inputdir with the -i or --inputdir option')
Here the parser.error()
method will print the help information together with your error message, then exit.
这篇关于如何使用 Python Argparse 制作所需参数的短版和长版?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!