如何使用 Python Argparse 制作所需参数的短版本和长版本?
How to make a short and long version of a required argument using Python Argparse?
我想指定一个名为 inputdir
的必需参数,但我还想要一个名为 i
的 shorthand 版本。我没有看到一个简洁的解决方案来执行此操作,而不是同时提供两个可选参数然后进行我自己的检查。是否有我没有看到的首选做法,或者唯一的方法是将两者都设为可选并进行我自己的错误处理?
这是我的代码:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("inputdir", help="Specify the input directory")
parser.parse_args()
对于 flags(以 -
或 --
开头的选项)传入选项 和 标志。您可以指定多个选项:
parser.add_argument('-i', '--inputdir', help="Specify the input directory")
参见 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.
演示:
>>> 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')
但是,必需 参数的第一个元素只是一个占位符。 -
和 --
选项 总是 可选(这是命令行约定),此类开关永远不会指定必需的参数。相反,命令行帮助将根据传递给 add_argument()
的第一个参数显示在何处放置所需参数和占位符,该参数将在没有破折号的情况下传递。
如果您必须打破该约定并使用以 -
或 --
开头的参数 无论如何 ,您将不得不自己检查:
args = parser.parse_args()
if not args.inputdir:
parser.error('Please specify an inputdir with the -i or --inputdir option')
这里parser.error()
method会打印帮助信息和你的错误信息,然后退出。
https://docs.python.org/3/library/argparse.html#required
required=True 应该支持使参数成为必需的
我想指定一个名为 inputdir
的必需参数,但我还想要一个名为 i
的 shorthand 版本。我没有看到一个简洁的解决方案来执行此操作,而不是同时提供两个可选参数然后进行我自己的检查。是否有我没有看到的首选做法,或者唯一的方法是将两者都设为可选并进行我自己的错误处理?
这是我的代码:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("inputdir", help="Specify the input directory")
parser.parse_args()
对于 flags(以 -
或 --
开头的选项)传入选项 和 标志。您可以指定多个选项:
parser.add_argument('-i', '--inputdir', help="Specify the input directory")
参见 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.
演示:
>>> 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')
但是,必需 参数的第一个元素只是一个占位符。 -
和 --
选项 总是 可选(这是命令行约定),此类开关永远不会指定必需的参数。相反,命令行帮助将根据传递给 add_argument()
的第一个参数显示在何处放置所需参数和占位符,该参数将在没有破折号的情况下传递。
如果您必须打破该约定并使用以 -
或 --
开头的参数 无论如何 ,您将不得不自己检查:
args = parser.parse_args()
if not args.inputdir:
parser.error('Please specify an inputdir with the -i or --inputdir option')
这里parser.error()
method会打印帮助信息和你的错误信息,然后退出。
https://docs.python.org/3/library/argparse.html#required
required=True 应该支持使参数成为必需的