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

前端 未结 3 618
粉色の甜心
粉色の甜心 2020-12-07 07:12

Suppose I have the following argparse snippet:

diags.cmdln_parser.add_argument( \'--scan-time\',
                     action  = \'store\',
                           


        
3条回答
  •  情书的邮戳
    2020-12-07 07:57

    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)
    

提交回复
热议问题