Python argparse 互斥组 [英] Python argparse mutual exclusive group

查看:42
本文介绍了Python argparse 互斥组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是:

pro [-a xxx |[-b yyy -c zzz]]

我试过了,但没有用.有人可以帮我吗?

group= parser.add_argument_group('模型 2')group_ex = group.add_mutually_exclusive_group()group_ex.add_argument("-a", type=str, action = "store", default = "", help="test")group_ex_2 = group_ex.add_argument_group("选项 2")group_ex_2.add_argument("-b", type=str, action = "store", default = "", help="test")group_ex_2.add_argument("-c", type=str, action = "store", default = "", help="test")

谢谢!

解决方案

add_mutually_exclusive_group 不会使整个组互斥.它使组内的选项相互排斥.

您要查找的是子命令.而不是 prog [ -a xxxx |[-b yyy -c zzz]],你会:

prog命令 1-一种: ...命令 2-b: ...-C: ...

使用第一组参数调用:

prog command_1 -a xxxx

使用第二组参数调用:

prog command_2 -b yyyy -c zzzz

您还可以将子命令参数设置为位置.

prog command_1 xxxx

有点像 git 或 svn:

git commit -amgit合并开发

工作示例

#创建顶级解析器解析器 = argparse.ArgumentParser(prog='PROG')parser.add_argument('--foo', action='store_true', help='help for foo arg.')subparsers = parser.add_subparsers(help='子命令的帮助')# 为command_1"命令创建解析器parser_a = subparsers.add_parser('command_1', help='command_1 help')parser_a.add_argument('a', type=str, help='help for bar, positional')# 为command_2"命令创建解析器parser_b = subparsers.add_parser('command_2', help='help for command_2')parser_b.add_argument('-b', type=str, help='help for b')parser_b.add_argument('-c', type=str, action='store', default='', help='test')

测试一下

<预><代码>>>>parser.print_help()用法:PROG [-h] [--foo] {command_1,command_2} ...位置参数:{command_1,command_2}子命令帮助command_1 command_1 帮助command_2 的 command_2 帮助可选参数:-h, --help 显示此帮助信息并退出--foo 帮助 foo arg.>>>>>>parser.parse_args(['command_1', '工作'])命名空间(a='working', foo=False)>>>parser.parse_args(['command_1', 'wellness', '-b x'])用法:PROG [-h] [--foo] {command_1,command_2} ...PROG:错误:无法识别的参数:-b x

祝你好运.

What I need is:

pro [-a xxx | [-b yyy -c zzz]]

I tried this but does not work. Could someone help me out?

group= parser.add_argument_group('Model 2')
group_ex = group.add_mutually_exclusive_group()
group_ex.add_argument("-a", type=str, action = "store", default = "", help="test")
group_ex_2 = group_ex.add_argument_group("option 2")
group_ex_2.add_argument("-b", type=str, action = "store", default = "", help="test")
group_ex_2.add_argument("-c", type=str, action = "store", default = "", help="test")

Thanks!

解决方案

add_mutually_exclusive_group doesn't make an entire group mutually exclusive. It makes options within the group mutually exclusive.

What you're looking for is subcommands. Instead of prog [ -a xxxx | [-b yyy -c zzz]], you'd have:

prog 
  command 1 
    -a: ...
  command 2
    -b: ...
    -c: ...

To invoke with the first set of arguments:

prog command_1 -a xxxx

To invoke with the second set of arguments:

prog command_2 -b yyyy -c zzzz

You can also set the sub command arguments as positional.

prog command_1 xxxx

Kind of like git or svn:

git commit -am
git merge develop

Working Example

# create the top-level parser
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('--foo', action='store_true', help='help for foo arg.')
subparsers = parser.add_subparsers(help='help for subcommand')

# create the parser for the "command_1" command
parser_a = subparsers.add_parser('command_1', help='command_1 help')
parser_a.add_argument('a', type=str, help='help for bar, positional')

# create the parser for the "command_2" command
parser_b = subparsers.add_parser('command_2', help='help for command_2')
parser_b.add_argument('-b', type=str, help='help for b')
parser_b.add_argument('-c', type=str, action='store', default='', help='test')

Test it

>>> parser.print_help()
usage: PROG [-h] [--foo] {command_1,command_2} ...

positional arguments:
  {command_1,command_2}
                        help for subcommand
    command_1           command_1 help
    command_2           help for command_2

optional arguments:
  -h, --help            show this help message and exit
  --foo                 help for foo arg.
>>>

>>> parser.parse_args(['command_1', 'working'])
Namespace(a='working', foo=False)
>>> parser.parse_args(['command_1', 'wellness', '-b x'])
usage: PROG [-h] [--foo] {command_1,command_2} ...
PROG: error: unrecognized arguments: -b x

Good luck.

这篇关于Python argparse 互斥组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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