Argparse: Way to include default values in '--help'?
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)
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 informat
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.