how to add_argument_group to add_mutually_exclusive_group with python argparse

前端 未结 1 1995
南旧
南旧 2021-02-08 04:01

I am trying to implement the following:

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

However, no matte

1条回答
  •  醉梦人生
    2021-02-08 04:27

    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.


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

    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.

    The resulting usage is:

    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.

    It accepts inputs like:

    ''
    '-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.

    0 讨论(0)
提交回复
热议问题