I'm writing a command-line utility using Argparse and have added a bunch of sub_parsers (sub commands). In the help menu they appear under a group called "commands" and I get a nice list of all the possible options. However before this list appears, all the same commands appear under the group title in braces like so:
Commands:
{foo, bar}
foo - foo does foo
bar - bar does bar
I want to remove the redundant entries which appear in braces. It only appears in this group which is filled with sub_parsers.
My code to handle this looks like so: (where parser is the ArgumentParser() instance)
subparsers = parser.add_subparsers(title="Commands")
foo = subparsers.add_parser("foo", help="- foo does foo")
bar = subparsers.add_parser("bar", help="- bar does bar")
I've looked at the attributes and methods of my commands action group and can't seem to find anything that will solve this for me (at least from what I can make sense of). I'm not sure if anyone else has dealt with this, I realize it's probably a bit obscure. And again, all I'm trying to do is find the way to remove the redundant list of the commands which appear in braces.
The "{foo,bar}" part is the argument 'metavar'. A metavar is how argparse refers to expected argument values in the usage and help strings. argparse treats subcommands like an argument with multiple choices so if you don't specify a metavar, the default is the list of choices (subcommands) in curly braces. It lets the user know the possible options for subcommands but since they're listed just below, it's redundant and if you have lots of subcommands, it's ugly.
You can easily replace with your own chosen metavar:
subparsers = parser.add_subparsers(title="Commands", metavar="<command>")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With