The argparse argument generated help, "metavar" with a choice

When using an argument (optional and positional, both have this problem) with the choices , the generated help output shows these options.

If the same argument also includes the metavar keyword, the list of options is excluded from the generated output.

What I had in mind was to show metavar in the usage line, but actually show the available options when "autohelp" contains data about the positional / optional argument.

Any simple fixes / workarounds?


I have already created the argparse wrapper for user help. Perhaps this will be another feature on my TODO list.

+6
source share
1 answer

You can add options to the help text.

 parser=argparse.ArgumentParser() parser.add_argument('-f',metavar="TEST",choices=('a','b','c'), help='choices, {%(choices)s}') print parser.format_help() 

gives:

 usage: stack20328931.py [-h] [-f TEST] optional arguments: -h, --help show this help message and exit -f TEST choices, {a, b, c} 
+11
source

Source: https://habr.com/ru/post/959171/


All Articles