0

Having a bit of problem with argparse in Python...

import argparse

parser = argparse.ArgumentParser()
parser.add_argument ("-o", "--optional", help="this is an optional argument")
args = parser.parse_args()

print ( args.optional )

Calling test.py -h will output...

usage: test.py [-h] [-o OPTIONAL]

optional arguments:
  -h, --help            show this help message and exit
  -o OPTIONAL, --optional OPTIONAL
                        this is an optional argument

Is there any way I can get rid of the extra OPTIONALs in the help menu? I know I could do this with parser.add_argument ("-o", "--optional", help="this is an optional argument", action=store_true), except I can't because I need to call args.optional later on.

Again, this isn't so much about the functionality of the program as the aesthetics because test.py -o hello would print hello.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • If I understand you correctly you want to use a metavar parameter: http://stackoverflow.com/questions/19124304/what-does-metavar-and-action-mean-in-argparse-in-python – Michel Müller Jan 06 '17 at 04:18

2 Answers2

0

Usually an option with no arguments has an action, which will suppress that metavar:

parser.add_argument ("-o", "--optional", action='store_true')

Otherwise, you could amend the argument like this:

parser.add_argument ("-o", "--optional", metavar='', help="the help text")
wim
  • 338,267
  • 99
  • 616
  • 750
  • I thought that's what you wanted. Please add your desired output to the question. – wim Jan 06 '17 at 05:01
  • sorry, I used double quotes instead of single which messed up the output. Using single quotes it executes properly. Many thanks! – Chuck Finley Jan 06 '17 at 05:11
0

First is this about the parsing of this argument or the display in the help?

parser.add_argument ("-o", "--optional", help="this is an optional argument")

has the default store_true action, and thus takes one argument. As indicated in the usage:

usage: test.py [-h] [-o OPTIONAL]

where 'OPTIONAL' is a standin for the string that you will include after the -o or --optional. And args.optional will have the value of that string.

action='store_true turns this argument into a boolean, False if not given, True if -o is provided. It takes to no added value.

-o OPTIONAL, --optional OPTIONAL

is the normal way an Action like this is displayed in the help. Again OPTIONAL is the place marker for the string that follows -o or --optional. A metavar parameter can be used to customize that place marker. It can be as short as "".

Some people don't like that repeated pattern, preferring something like

-o , --optional OPTIONAL

That has been discussed in previous questions. It requires a change to the HelpFormatter class (ie. a subclassing).

python argparse help message, disable metavar for short options?

Another thing is to simplify the definition

parser.add_argument ("-o", dest="optional", help="this is an optional argument")
Community
  • 1
  • 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353