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

查看:26
本文介绍了如何使用python argparse将_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 正在尝试创建更通用的用法 组.

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

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,...)

这将mutually_exclusive_group 替换为可以嵌套的usage_group,并且可以测试除'xor' 之外的其他逻辑关系.'any' kind 接受其参数的任意组合,就像您期望的 'argument_group' 一样.

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将_argument_group添加到add_mutually_exclusive_group的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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