问题
I'm not sure if I've found an argparse bug, or if I'm being obtuse. I researched #45602511 which is how I found it "works" in the top-level case.
I'm trying to use an argument_group to separate out some formatting options (which happen to be mutually exclusive). If things are at the top level of the parser structure, I can nest a mutually_exclusive_group
under an argument_group
and I get the help behavior I'm expecting:
The following code works fine in Python 3.6.2:
import argparse
parser = argparse.ArgumentParser()
commands = parser.add_subparsers()
hit = commands.add_parser('hit')
miss = commands.add_parser('miss')
parser.add_argument('--summary', action='store_true', help='summarize information')
parser.add_argument('--verbose', action='store_true', help='tell us more')
output_format = parser.add_argument_group("Output format")
styles = output_format.add_mutually_exclusive_group()
styles.add_argument('--plain', dest='style')
styles.add_argument('--green', dest='style')
styles.add_argument('--blue', dest='style')
print(parser.parse_args(['-h']))
which generates the following output:
usage: baz.py [-h] [--summary] [--verbose]
[--plain STYLE | --green STYLE | --blue STYLE]
{hit,miss} ...
positional arguments:
{hit,miss}
optional arguments:
-h, --help show this help message and exit
--summary summarize information
--verbose tell us more
Output format:
--plain STYLE
--green STYLE
--blue STYLE
However, I actually need this help code for a subparser, not at the top level, so my code really looks like this, and argparse ignores the argument_group nesting:
global_options = argparse.ArgumentParser(add_help=False)
global_options.add_argument('--summary', action='store_true', help='summarize information')
global_options.add_argument('--verbose', action='store_true', help='tell us more')
output_format = global_options.add_argument_group("Output format", "ways to foo")
styles = output_format.add_mutually_exclusive_group()
styles.add_argument('--plain', dest='style')
styles.add_argument('--green', dest='style')
styles.add_argument('--blue', dest='style')
parser = argparse.ArgumentParser()
commands = parser.add_subparsers()
hit = commands.add_parser('hit', parents=[global_options])
miss = commands.add_parser('miss', parents=[global_options])
print(parser.parse_args(['hit', '-h']))
producing:
usage: bar.py hit [-h] [--summary] [--verbose]
[--plain STYLE | --green STYLE | --blue STYLE]
optional arguments:
-h, --help show this help message and exit
--summary summarize information
--verbose tell us more
--plain STYLE
--green STYLE
--blue STYLE
Output format:
ways to foo
Am I doing something wrong, or is argparse broken?
回答1:
This problem of a nested mutually exclusive group in a parents
has been raised in a couple of bug/issues.
http://bugs.python.org/issue25882, (argparse help error: arguments created by add_mutually_exclusive_group() are shown outside their parent group created by add_argument_group())
and
http://bugs.python.org/issue16807, (argparse group nesting lost on inheritance)
Nesting a mutually exclusive group in an argument group works, but isn't documented. But there is an example of this in the unittesting file, http://bugs.python.org/issue17218, (support title and description in argparse add_mutually_exclusive_group)
Copying groups and actions from a parents
is handled by parser._add_container_actions
method. The parents
mechanism works fine for simple parsers, but it is not heavily used nor is it robust.
Sample subparser creation function
def make_subparser(commands, name):
sub = commands.add_parser(name)
sub.add_argument('--summary', action='store_true', help='summarize information')
sub.add_argument('--verbose', action='store_true', help='tell us more')
output_format = sub.add_argument_group("Output format", "ways to foo")
styles = output_format.add_mutually_exclusive_group()
styles.add_argument('--plain', dest='style')
styles.add_argument('--green', dest='style')
styles.add_argument('--blue', dest='style')
return sub
parser = argparse.ArgumentParser()
commands = parser.add_subparsers()
hit = make_subparser(commands,'hit')
miss = make_subparser(commands 'miss')
来源:https://stackoverflow.com/questions/46066374/python-argparse-mutually-exclusive-group-and-add-argument-group-in-a-parent-proc