Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argparse: Way to include default values in '--help'?

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?

like image 264
JS. Avatar asked Aug 28 '12 00:08

JS.


People also ask

Is Argparse the default?

The argparse module will automatically allow an option -h or --help that prints a usage string for all the registered options. By default, the type is str , the default value is None , the help string is empty, and metavar is the option in upper case without initial dashes.

What is Argparse ArgumentParser ()?

The argparse module provides a convenient interface to handle command-line arguments. It displays the generic usage of the program, help, and errors. The parse_args() function of the ArgumentParser class parses arguments and adds value as an attribute dest of the object.

How do you add an optional argument in Argparse?

To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.


2 Answers

Use the argparse.ArgumentDefaultsHelpFormatter formatter:

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

To quote the documentation:

The other formatter class available, ArgumentDefaultsHelpFormatter, will add information about the default value of each of the arguments.

Note that this only applies to arguments that have help text defined; with no help value for an argument, there is no help message to add information about the default value to.

The exact output for your scan-time option then becomes:

  --scan-time [SCAN_TIME]                         Wait SCAN-TIME seconds between status checks.                         (default: 5) 
like image 126
Martijn Pieters Avatar answered Oct 03 '22 13:10

Martijn Pieters


Add '%(default)s' to the help parameter to control what is displayed.

parser.add_argument("--type", default="toto", choices=["toto","titi"],                               help = "type (default: %(default)s)") 

Notes:

  • It is %+ default in parenthesis + format characters (not to be confused with curly brackets {default} we find in format or f-string)
  • Don't forget to add the "specifier character" for the type representation at the end (i.e. s for strings, d for integers, f for floats, etc.)
  • You can also add the usual "printf" format specifiers (like number of digits for floats, leading zeros, etc.)

You can refer to printf documentation for more details.

like image 20
polux.moon Avatar answered Oct 03 '22 13:10

polux.moon