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?
Argparse Module The argparse module in Python helps create a program in a command-line-environment in a way that appears not only easy to code but also improves interaction. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments. Steps for Using Argparse
The default value would be blue if --color argument is not defined. With python argparse, you must declare your positional arguments explicitly. If you do not, the parser expects to have no arguments left over after it completes parsing, and it raises an error if arguments still remain.
With python argparse, you must declare your positional arguments explicitly. If you do not, the parser expects to have no arguments left over after it completes parsing, and it raises an error if arguments still remain. How do we define optional and positional arguments?
required¶ In general, the argparse module assumes that flags like -f and --bar indicate optional arguments, which can always be omitted at the command line. To make an option required, True can be specified for the required= keyword argument to add_argument(): >>>
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
.
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