How to include default values in ‘–help’

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

Output looks like this:

1
2
3
  --scan-time [SCAN_TIME]
                        Wait SCAN-TIME seconds between status checks.
                        (default: 5)

Another tip: add ‘%(default)’ to the help parameter to control what is displayed.

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

source