If I populate an exclusively mutual argparse group and then parser.add_argument (for other items) the usage shows the arguments within the exclusively mutual group as mutually exclusive:
import argparse
parser1 = argparse.ArgumentParser()
group1 = parser1.add_mutually_exclusive_group()
group1.add_argument('--start')
group1.add_argument('--stop')
group1.add_argument('--restart')
parser1.add_argument('--os')
parser1.print_usage()
Output:
usage: arg_usage.py [-h] [--start START | --stop STOP | --restart RESTART] [--os OS]
However if I add a non-mutually exclusive argument to the parser in the middle of the group the usage output no longer reflects the fact that some of the arguments are mutually exclusive:
import argparse
parser2 = argparse.ArgumentParser()
group2 = parser2.add_mutually_exclusive_group()
group2.add_argument('--start')
group2.add_argument('--stop')
parser2.add_argument('--os')
group2.add_argument('--restart')
parser2.print_usage()
Output:
usage: arg_usage.py [-h] [--start START] [--stop STOP] [--os OS]
[--restart RESTART]
Is there any way to work around this without manually supplying the usage string or re-ordering the calls?
Additional note - I've found the same issue occurs (usage doesn't indicate mutually exclusive options) when adding mixed types to a mutually exclusive group (positional & optional).
Additional ?: Are these bugs that should be submitted to http://bugs.python.org/ ?
Why would you place the parser
argument in the middle of the group
arguments if you didn't want them listed in the usage
in that order? Since optionals
can be parsed in any order, usage
is the only reason to define them in any particular order.
This is the result of how parser.format_usage
generates the usage
line. It lists the arguments in the order that you defined them, with one modification - positionals are listed after optionals. It includes the mutually exclusive group markings only if they can be overlayed on this defined order.
If your group includes a mix of optionals and a positional (can't have more than one positional in a group), then that group will only be marked if that positional comes immediately after the optionals.
There is a bug request for the ability to add existing arguments to a new mutually exclusive group. http://bugs.python.org/issue10984 . That is easily done. But getting the usage right is difficult. If an argument belongs to more than one group, it is impossible to display both groups without repeating the argument. That prompted me to rewrite the usage formatter. In this alternate version, each group is formatted, repeating arguments as needed.
In short, changing this formatting behavior requires a major rewrite of the usage code. Until that is patched, you will have to settle for writing your own usage
line. That way you get to give group order priority over definition order.
Look at http://bugs.python.org/issue11588 is you want to puzzle over the tougher task of formatting usage when groups can be nested and include logic other than 'xor' (eg mutually inclusive groups).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.