如何根据 python 中的某些条件在 argparser 中使位置参数可选
How to make positional argument optional in argparser based on some condition in python
我想编写 python 代码,其中基于从命令行传递的一些参数,我想将位置参数设为可选。
例如,
我的 python 程序是 test.py,我可以用它来提供 --init、--snap、--check 选项。现在,如果我给出了 --snap 和 --check 选项,那么文件名是强制性的,即
test.py --快照文件1
但是如果我给了 --init 选项那么它不应该接受任何其他参数。即在这种情况下文件名是可选的:
test.py --init
如何实现这个条件
如果您可以稍微更改命令行,那么一组子解析器应该可以工作。
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='sub-command help')
init_parser = subparsers.add_parser('init', help='do the init stuff')
snap_parser = subparsers.add_parser('snap', help='do the snap stuff')
snap_parser.add_argument('--file', '-f', required=True)
check_parser = subparsers.add_parser('check', help='do the check stuff')
check_parser.add_argument('--file', '-f', required=True)
args = parser.parse_args()
print args
然后输出...
> python foobar.py init
Namespace()
> python foobar.py check
usage: foobar.py check [-h] --file FILE
foobar.py check: error: argument --file/-f is required
> python foobar.py check --file foobar.txt
Namespace(file='foobar.txt')
一般帮助:
> python foobar.py --help
usage: foobar.py [-h] {init,snap,check} ...
positional arguments:
{init,snap,check} sub-command help
init do the init stuff
snap do the snap stuff
check do the check stuff
optional arguments:
-h, --help show this help message and exit
以及具体的子命令帮助
> python foobar.py snap -h
usage: foobar.py snap [-h] --file FILE
optional arguments:
-h, --help show this help message and exit
--file FILE, -f FILE
您的另一个选择是使用 nargs
,正如@1.618 已经提到的那样。
argparse 允许您指定某些参数有自己的参数,如下所示:
parser.add_argument("--snap", nargs=1)
或使用 +
允许任意数量的 "subargs"
调用 parse_args()
后,值将在列表中:
filename = args.snap[0]
我想编写 python 代码,其中基于从命令行传递的一些参数,我想将位置参数设为可选。
例如, 我的 python 程序是 test.py,我可以用它来提供 --init、--snap、--check 选项。现在,如果我给出了 --snap 和 --check 选项,那么文件名是强制性的,即 test.py --快照文件1
但是如果我给了 --init 选项那么它不应该接受任何其他参数。即在这种情况下文件名是可选的: test.py --init
如何实现这个条件
如果您可以稍微更改命令行,那么一组子解析器应该可以工作。
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='sub-command help')
init_parser = subparsers.add_parser('init', help='do the init stuff')
snap_parser = subparsers.add_parser('snap', help='do the snap stuff')
snap_parser.add_argument('--file', '-f', required=True)
check_parser = subparsers.add_parser('check', help='do the check stuff')
check_parser.add_argument('--file', '-f', required=True)
args = parser.parse_args()
print args
然后输出...
> python foobar.py init
Namespace()
> python foobar.py check
usage: foobar.py check [-h] --file FILE
foobar.py check: error: argument --file/-f is required
> python foobar.py check --file foobar.txt
Namespace(file='foobar.txt')
一般帮助:
> python foobar.py --help
usage: foobar.py [-h] {init,snap,check} ...
positional arguments:
{init,snap,check} sub-command help
init do the init stuff
snap do the snap stuff
check do the check stuff
optional arguments:
-h, --help show this help message and exit
以及具体的子命令帮助
> python foobar.py snap -h
usage: foobar.py snap [-h] --file FILE
optional arguments:
-h, --help show this help message and exit
--file FILE, -f FILE
您的另一个选择是使用 nargs
,正如@1.618 已经提到的那样。
argparse 允许您指定某些参数有自己的参数,如下所示:
parser.add_argument("--snap", nargs=1)
或使用 +
允许任意数量的 "subargs"
调用 parse_args()
后,值将在列表中:
filename = args.snap[0]