python 位置参数用法_python:互斥的位置参数

该博客探讨了如何在Python的argparse模块中创建互斥的位置参数。作者当前的实现允许命令行选项,但无法同时作为位置参数。他们尝试了使用子解析器的方法,但这改变了命令行用法的格式。作者寻求一种方法,既能保留位置参数,又能实现互斥性,同时保持理想的命令行帮助输出格式。解决方案可能涉及自定义argparse行为或调整现有构造的使用。
摘要由CSDN通过智能技术生成

我希望我的程序接受互斥的位置参数,和用法显示为一组参数。

目前我只能做到这一点,但不能两者兼而有之。。。在

这是我目前拥有的: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()

得出如下结论:

^{pr2}$

但我真正想要的是query和{}是两个互斥的位置参数。在

我试着用下面的小广告: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)

{是互斥的

我知道ArgumentParser.add_mutually_exclusive_group(required=False)方法,但是使用它并不能帮助实现我想要的结果,因为1)使用它时参数必须是可选的,并且2)usage格式不是我想要的。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值