Argparse argument generated help, 'metavar' with choices

argparse, python

Solution

You can add the choices 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()

produces:

usage: stack20328931.py [-h] [-f TEST]

optional arguments:
  -h, --help  show this help message and exit
  -f TEST     choices, {a, b, c}

Problem

When using an argument (optional and positional both have this problem) with the keyword `choices`, the generated help output shows those choices. If that same argument also includes a `metavar` keyword, the list of choices is omitted from the generated output. What I had in mind, was to show the `metavar` in the `usage` line, but actually show the available choices when the 'autohelp' lists positional/optional argument details. Any simple fixes/workarounds? I have already started an argparse wrapper for custom help functionality. Perhaps this should be another feature on my TODO list.

Original source