如何使用python argparse将add_argument_group添加到add_mutually_exclusive_group [英] how to add_argument_group to add_mutually_exclusive_group with python argparse

查看:302
本文介绍了如何使用python argparse将add_argument_group添加到add_mutually_exclusive_group的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以下目标:

I am trying to implement the following:

$ prog.py -h
usage: prog.py [-h] [-s | -m] [[-y [year]] | [[-1 | -3] [month] [year]]]

但是,无论我如何使用add_argument_group和add_mutually_exclusive_group

However, no matter how I played with add_argument_group and add_mutually_exclusive_group,

#!/usr/bin/env python

import argparse

def main(opt):
    print(opt)

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    bar = parser.add_mutually_exclusive_group()
    bar.add_argument('-s', action='store_true', default=True)
    bar.add_argument('-m', action='store_true', default=False)

    #bar = parser.add_argument_group()
    bar = parser.add_mutually_exclusive_group()
    bar.add_argument('-y', metavar='year', type=int,
                     dest='iy', nargs='?', default=0)
    baz = bar.add_argument_group()
    g_13 = baz.add_mutually_exclusive_group()
    g_13.add_argument('-1', dest='i1',
                      help='Display single month output.',
                      action='store_true', default=True)
    g_13.add_argument('-3', dest='i3',
                      help='Display prev/current/next month output.',
                      action='store_true', default=False)
    #aaa = bar.add_argument_group()
    baz.add_argument(metavar='month', type=int,
                        choices=range(1, 13),
                        dest='mo', nargs='?', default=1)
    baz.add_argument(metavar='year', type=int,
                        dest='yr', nargs='?', default=2000)

    main(parser.parse_args())

我只能管理:

$ prog.py -h
usage: prog.py [-h] [-s | -m] [-y [year]] [-1 | -3] [month] [year]

也就是说,我不能将[-y [year]][[-1 | -3] [month] [year]]分为互斥组.我不知道为什么.有人可以帮忙吗?谢谢.

That is, I cannot group [-y [year]] and [[-1 | -3] [month] [year]] into a mutually exclusive group. I cannot figure out why. Could anyone help? Thanks.

推荐答案

参数组仅用于帮助组织帮助显示.它们不能嵌套.互斥组将测试参数并修改用法显示.它们可以嵌套,但最终结果与组成一个大组的结果相同. http://bugs.python.org/issue11588 试图创建一个更通用的usage

Argument groups just help organize the help display. They cannot be nested. Mutually exclusive groups test arguments and modify the usage display. They can be nested, but the end result is the same as if you made one big group. http://bugs.python.org/issue11588 is trying to create a more general purpose usage group.

同时,如果互斥组没有足够的控制权,您可以编写一个自定义用法行,并在解析后测试参数.

In the mean time you can write a custom usage line, and test the arguments after parsing, if mutually exclusive groups don't give you enough control.

这是使用我为 http://bugs.python.org/issue11588 开发的代码的解析器a>

Here's a parser using code that I'm developing for http://bugs.python.org/issue11588

parser = argparse.ArgumentParser(formatter_class=argparse.UsageGroupHelpFormatter)

bar = parser.add_usage_group(kind='mxg', dest='s|m')
bar.add_argument('-s', action='store_true', default=True)
bar.add_argument('-m', action='store_true', default=False)

bar = parser.add_usage_group(kind='mxg', dest='year|more')
bar.add_argument('-y', metavar='year', type=int,...)

baz = bar.add_usage_group(kind='any', dest='', joiner=' ', parens='[]')

g_13 = baz.add_usage_group(kind='mxg', dest='1|3')
g_13.add_argument('-1', dest='i1',...)
g_13.add_argument('-3', dest='i3',...)

baz.add_argument(metavar='month', type=int,...)
baz.add_argument(metavar='year', type=int,...)

这用usage_group替换了mutually_exclusive_group,该usage_group可以嵌套,并且可以测试除'xor'之外的其他逻辑关系.就像您所期望的"argument_group"一样,"any"类型接受其参数的任何组合.

This replaces mutually_exclusive_group with usage_group which can be nested, and can test for other logical relations besides 'xor'. 'any' kind accepts any combination of its arguments, much as you expected the 'argument_group' to act.

最终的用法是:

usage: prog.py [-h] [-s | -m] [-y [year] | [[-1 | -3] month year]] [month]
           [year]

主要故障是位置信息的显示月"和年".它们在任意"组中的正确位置,但它们还会显示在通常的位置位置尾随位置.

The main fault is the display of the positionals, 'month' and 'year'. They are in the right place in the 'any' group, but they also display in the usual trailing location for positionals.

它接受如下输入:

''
'-y 1943 -s
'-1 12 1943'
'12 1943'
'12'
'-3'

'1943' gives an error, because it it is out of range for a month

如您所见,扩展组的概念并非易事.我认为我走在正确的轨道上,但是仍然有粗糙的边缘.

As you can see expanding the concept of groups is not trivial. I think I'm on the right track, but there are still rough edges.

这篇关于如何使用python argparse将add_argument_group添加到add_mutually_exclusive_group的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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