我希望我的程序接受相互排斥的位置参数,并将用法显示为一组参数。
目前我只能实现一个或另一个,但不是两者都。。。
这就是我现在拥有的:
def parse_arguments():
arg_parser = argparse.ArgumentParser(description = 'Project Builder')
query_parser = arg_parser.add_argument_group('query', "Query current state")
build_parser = arg_parser.add_argument_group('build', "Build project")
# query arguments
query_parser.add_argument('-s', '--servers',
action = 'store_true',
required = False,
help = 'Display available servers')
query_parser.add_argument('-u', '--users',
action = 'store_true',
required = False,
help = 'Display current users')
# build arguments
build_parser.add_argument('-f', '--force',
action = 'store',
required = False,
metavar = 'SERVER_NAME',
help = 'Force build on SERVER_NAME')
build_parser.add_argument('-c', '--clean',
action = 'store_true',
required = False,
help = 'Clean repo before build')
build_parser.add_argument('-v', '--verbosity',
action = 'store_true',
required = False,
help = 'Print stderr to console')
build_parser.add_argument('-p', '--project',
action = 'store',
required = True,
metavar = 'project_A|project_B|project_C',
type = project_name,
help = 'Project to build (required)')
return vars(arg_parser.parse_args())
args = parse_arguments()
它给出了以下信息:
usage: test.py [-h] [-s] [-u] [-f SERVER_NAME] [-c] [-v] -p
project_A|project_B|project_C
Project Builder
optional arguments:
-h, --help show this help message and exit
query:
Query current state
-s, --servers Display available servers
-u, --users Display current users
build:
Build project
-f SERVER_NAME, --force SERVER_NAME
Force build on SERVER_NAME
-c, --clean Clean repo before build
-v, --verbosity Print stderr to console
-p project_A|project_B|project_C, --project project_A|project_B|project_C
Project to build (required)
但我真正想要的是
query
和build
是两个相互排斥的位置参数。我试着用了如下的子文件:
def parse_arguments():
arg_parser = argparse.ArgumentParser(description = 'Project Builder')
command_parser = arg_parser.add_subparsers(help = "Command")
query_parser = command_parser.add_parser('query', help = "Query current state")
build_parser = command_parser.add_parser('build', help = "Build project")
# query arguments
query_parser.add_argument('-s', '--servers',
action = 'store_true',
required = False,
help = 'Display available servers')
query_parser.add_argument('-u', '--users',
action = 'store_true',
required = False,
help = 'Display current users')
# build arguments
build_parser.add_argument('-f', '--force',
action = 'store',
required = False,
metavar = 'SERVER_NAME',
help = 'Force build on SERVER_NAME')
build_parser.add_argument('-c', '--clean',
action = 'store_true',
required = False,
help = 'Clean repo before build')
build_parser.add_argument('-v', '--verbosity',
action = 'store_true',
required = False,
help = 'Print stderr to console')
build_parser.add_argument('-p', '--project',
action = 'store',
required = True,
metavar = 'project_A|project_B|project_C',
type = project_name,
help = 'Project to build (required)')
return vars(arg_parser.parse_args())
但这会产生以下结果:
usage: test.py [-h] {query,build} ...
Project Builder
positional arguments:
{query,build} Command
query Query current state
build Build project
optional arguments:
-h, --help show this help message and exit
我想要的是上述两种尝试的结合,即:
usage: test.py [-h] {query,build} ...
Project Builder
optional arguments:
-h, --help show this help message and exit
query:
Query current state
-s, --servers Display available servers
-u, --users Display current users
build:
Build project
-f SERVER_NAME, --force SERVER_NAME
Force build on SERVER_NAME
-c, --clean Clean repo before build
-v, --verbosity Print stderr to console
-p project_A|project_B|project_C, --project project_A|project_B|project_C
Project to build (required)
其中
query
和build
是互斥的。我知道
ArgumentParser.add_mutually_exclusive_group(required=False)
方法,但是使用它无助于实现我想要的,因为1)使用它时参数必须是可选的,2)格式不像我想要的那样。 最佳答案
在第一个场景中,您可以提供-p
选项
build_parser.add_argument('-p', '--project',
action = 'store', # default
required = True,
choices = ['project_A','project_B','project_C'],
# type = project_name, # doesn't make sense
help = 'Project to build (required)')
或者作为一个位置
build_parser.add_argument('project',
choices = ['project_A','project_B','project_C'],
help = 'Project to build')
type
必须是一个函数,它将字符串转换为您想要的内容,例如int('1')
,float('12.343')
。使用次纵火者是相似的。对于主解析器,subparser参数只是一个有选择的位置。但所采取的行动是将其余参数的解析退给子攻击者。
两个相互排斥的位置不合逻辑
`[foo | bar]`
它不能仅仅根据位置来判断您是想将字符串赋给
foo
还是bar
。但是您对基于值指定值感兴趣,或者更愿意将值限制为一组选项。如果不仔细查看它,您的子解析器代码看起来是正确的,并且应该根据需要解析输入。你测试过吗?
对子分析器的帮助显示并不像它可能的那样灵活,但是重做它需要相当数量的工作(这里有很多问题)。当前的设置是为主解析器和每个子解析器分别显示帮助。没有内置的全面帮助显示。
关于python - python:互斥的位置参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39180785/