Python: don't display 'choices' with argparse

argparse, parameters, python

Solution

Use the `metavar` argument::

parser.add_argument("-p", "--parameter", type=str, default=None, nargs='+',
                    help="some option",
                    choices=allValues.keys(),
                    metavar='PARAMETER'
                    )

This will give::

-p PARAMETER, --parameter PARAMETER some option

If you don't want to show a metavariable at all you could consider passing `''` to `metavar`. Otherwise, I believe you will have to create your own custom formatter classes and pass that to the `ArgumentParser`.

Problem

With argparse, I have the following line: ``` parser.add_argument("-p", "--parameter", type=str, default=None, nargs='+', help="some option", choices=allValues.keys() ) ``` The resulting `help` message shows all values in `allValues`: -p {a ,b ,c , d, e, f, g, h, i, l, m; a ,b ,c , d, e, f, g, h, i, l, m} [{a ,b ,c , d, e, f, g, h, i, l, m} ...], --parameter {a ,b ,c , d, e, f, g, h, i, l, m; a ,b ,c , d, e, f, g, h, i, l, m} [{a ,b ,c , d, e, f, g, h, i, l, m; a ,b ,c , d, e, f, g, h, i, l, m} ...] some option Can I remove `{a ,b ,c , d, e, f, g, h, i, l, m; a ,b ,c , d, e, f, g, h, i, l, m}` from above and just display the name of the parameter and the help message?

Original source