如何使用Python Argparse制作所需参数的简短版本? [英] How to make a short and long version of a required argument using Python Argparse?

查看:121
本文介绍了如何使用Python Argparse制作所需参数的简短版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想指定一个称为 inputdir 的必需参数,但我也想为其提供一个速记版本,称为 i 。如果没有两个可选参数,然后再进行自己的检查,我看不到一个简洁的解决方案。有没有我没有看到的首选方法,或者唯一的方法是使两者都可选,并自己处理错误?



这是我的代码:

  import argparse 
parser = argparse.ArgumentParser()
parser.add_argument( inputdir,帮助=指定输入目录)
parser.parse_args()


解决方案

用于标志(以--开头的选项)传递带有 标志的选项。您可以指定多个选项:

  parser.add_argument('-i','--inputdir',help =指定输入目录)

请参见 名称或标志选项文档



< blockquote>

add_argument()方法必须知道是否有可选参数,例如 -f -foo 或位置参数,例如文件名列表。因此,传递给 add_argument()的第一个参数必须是一系列标志或简单的参数名称。


Demo:

 >> import argparse 
>> parser = argparse.ArgumentParser()
>> parser.add_argument('-i','--inputdir',help =指定输入目录)
_StoreAction(option_strings = ['-i','--inputdir'],dest ='inputdir' ,nargs =无,const =无,默认=无,类型=无,选择=无,help =指定输入目录,metavar =无)
>> 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()的第一个参数,将所需的参数放在占位符的位置,该参数将不带破折号地传入。 / p>

如果您必须违反该约定并使用以--开头的参数-仍然需要 ,您必须自己进行检查:

  args = parser.parse_args()
如果不是args.inputdir:
parser.error('请使用-i或--i​​nputdir选项指定输入目录')

此处 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 to add_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屋!

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