My question is similar to argparse help without duplicate ALLCAPS question.
Though i would explain in brief what that question was and what my question is:
I'd like to display argparse help for my options the same way the default -h,--help
is, without the ALLCAPS text after each option, or at least without the duplicated CAPS.
For example, with the following code:
#filename=temp.py
import argparse
p = argparse.ArgumentParser()
p.add_argument('-i', '--ini', help="use alternate ini file")
print '\n', p.parse_args()
now running python temp.py -h
:
usage: temp.py [-h] [-i INI]
optional arguments:
-h, --help show this help message and exit
-i INI, --ini INI use alternate ini file
Now what I want is something like:
usage: 123.py [-h] [-i INI]
optional arguments:
-h, --help show this help message and exit
-i, --ini INI use alternate ini file
OR
usage: 123.py [-h] [-i INI]
optional arguments:
-h, --help show this help message and exit
-i, --ini use alternate ini file
To get the second one, the way is to change the default metavar
in the line p.add_argument
as:
p.add_argument('-i', '--ini', help="use alternate ini file")
and change the default usage statement in argparse.ArgumentParser()
.
But when number of optional arguments increases in my code, I find it difficult to modify the usage message by adding and deleting the argument according to the modification in my code.
Is there any other way of solving the problem of metavar
without affecting the usage statement.
Also what if I want to have my help displayed as shown in the first case, where there is only one time INI
after -i, --ini
.
If I am getting wrong in showing help as -i, --ini INI
or -i, --ini
instead of -i INI, --ini INI
please correct me with proper reason. (By getting wrong I mean, it the convention I am using will lead to confusion or misunderstanding in user)
https://stackoverflow.com/a/9643162/901925 and https://stackoverflow.com/a/23941599/901925 and https://stackoverflow.com/a/16969505/901925
give a
HelpFormatter._format_action_invocation(self, action)
method modification that replaces'-i INI, --ini INI'
with'-i, --ini INI'
.'-h, --help'
doesn't have the CAPS string because help does not take an argument. TheINI
is just a place holder for that argument. The original just tries to be clear, you can use either-i 124
or--ini 124
The
METAVAR
parameter gives you control over that place holder, but it is used both in formatting the usage and the help.If you don't want to go the custom HelpFormatter class route, you could still use the custom usage method. For example at some point during development, do
usage = parser.format_usage()
. Now change the parser so themetavar
is''
, and the usage is this new one.produces:
For a fast solution you can just set backspace character to a metavar.
It will get you this:
If you want this:
You will have to modify help formatter. Answered here python argparse help message, disable metavar for short options?