python - How to set the argparse subcommand order in the automatic description -
consider following piece of code:
$ cat a.py import argparse if __name__ == '__main__': parser = argparse.argumentparser() subparsers = parser.add_subparsers() parser_a = subparsers.add_parser('a', help='a') parser_b = subparsers.add_parser('b', help='b') parser_c = subparsers.add_parser('c', help='c') args = parser.parse_args() and simple invocation:
$ python a.py --help usage: a.py [-h] {a,c,b} ... positional arguments: {a,c,b} b b c c optional arguments: -h, --help show message , exit why positional arguments listed {a,c,b} instead of {a,b,c} , how can fix this? have 6 subcommands , looks ugly.
this python 2.7.7 fwiw.
can display (print), subparsers.choices? should ordereddict.
the {} produced .choices attribute of subparsers. while can iterable (list, dictionary, etc), current argparse code makes ordereddict, should display keys in order in added.
i'm not aware of change in behavior, @ least not in past couple of years. dig history.
you can change display giving subparsers metavar value, e.g.
subparsers = parser.add_subparsers(...., metavar='{a,b,c,d}') http://bugs.python.org/issue9026
is bug fix set ordereddict. in closed in march 2011.
Comments
Post a Comment