5

According to this question, this script called "parse_fail.py" should print default=1 when I type:

python parse_fail.py --help 

on the command line, but it doesn't. Why not?

#parse_fail.py

import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--test", type=int, dest='test', default=1)
parser.parse_args()
args_dict = vars(parser.parse_args())
locals().update(args_dict)
print test

When I run this script I get:

$ python parser_fail.py  --help
usage: parser_fail.py [-h] [--test TEST]

optional arguments:
  -h, --help   show this help message and exit
  --test TEST

EDIT: added output of the script.

Community
  • 1
  • 1
capybaralet
  • 1,757
  • 3
  • 21
  • 31

2 Answers2

7

Apparently you need to include a non-empty help string.

This works:

import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--test", type=int, dest='test', help="WHY NO DEFAULT???", default=1)
parser.parse_args()
args_dict = vars(parser.parse_args())
locals().update(args_dict)
print test



$ python parser_fail.py -h
usage: parser_fail.py [-h] [--test TEST]

optional arguments:
  -h, --help   show this help message and exit
  --test TEST  WHY NO DEFAULT??? (default: 1)
capybaralet
  • 1,757
  • 3
  • 21
  • 31
  • 1
    Simply including a non-empty help string did not work for me (Python 2.7.3). Using the `formatter_class=argparse.ArgumentDefaultsHelpFormatter` attribute when instantiating argparse.ArgumentParser as shown in @kindall's answer did the trick. – Trutane Dec 13 '19 at 00:34
0

You don't get that result because you haven't defined the variable test. Instead you get an error message, which actually tells you this. There is nothing in your code that defines test, so trying to print it is an error.

You are making two mistakes. First, you're throwing away the return value of parser.parse_args(), which is where your command line argument values are stored. To fix this, assign that to some variable.

args = parser.parse_args()

Second, you're trying to print test and not the test attribute of args (or whatever variable you used). To fix this, print args.test rather than test.

print args.test

Since you're only interested in the value of your test attribute, you could combine these (although, of course, you will usually want to use more than one argument value, so you probably wouldn't do this in a real script):

print parser.parse_args().test

Putting it together, this works:

import argparse
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--test", type=int, dest='test', default=1)
print parser.parse_args().test

(I can't really figure out how you got the code you posted from the link you gave, which is about including default values in help messages, but hopefully this helps anyway.)

kindall
  • 178,883
  • 35
  • 278
  • 309
  • With that formatter class the default should appear in the help message - but he hasn't showed us that. – hpaulj Apr 08 '16 at 03:53
  • 3
    This does not answer the question. The question is not about the value of the args object, but why the default value is missing from the help string. – episodeyang Sep 04 '20 at 08:03