Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Python by (47.6k points)

Suppose I have the following argparse snippet:

diags.cmdln_parser.add_argument( '--scan-time', action = 'store', nargs = '?', type = int, default = 5, help = "Wait SCAN-TIME seconds between status checks.")

Currently, --help returns:

usage: connection_check.py [-h] [--version] [--scan-time [SCAN_TIME]] Test the reliability/uptime of a connection. optional arguments: -h, --help show this help message and exit --version show program's version number and exit --scan-time [SCAN_TIME] Wait SCAN-TIME seconds between status checks.

I would prefer something like:

--scan-time [SCAN_TIME] Wait SCAN-TIME seconds between status checks. (Default = 5)

Peeking at the help formatter code revealed limited options. Is there a clever way to get argparse to print the default value for --scan-time in a similar fashion, or should I just subclass the help formatter?

1 Answer

0 votes
by (106k points)
edited by

The Way to include default values in '--help' is to use the argparse.ArgumentDefaultsHelpFormatter formatter below is the piece of code which will explain how to use it:

parser = argparse.ArgumentParser( # ... other options ... formatter_class=argparse.ArgumentDefaultsHelpFormatter)

To know more about this you can have a look at the following video:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 5, 2019 in Python by selena (1.6k points)
0 votes
1 answer

Browse Categories

...