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

前端 未结 3 587
粉色の甜心
粉色の甜心 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)
    
    0 讨论(0)
  • 2020-12-07 08:02

    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)")
    
    0 讨论(0)
  • 2020-12-07 08:07

    Wrapper class

    This is the most reliable and DRY approach I've found so far to both show defaults and use another formatter such as argparse.RawTextHelpFormatter at the same time:

    #!/usr/bin/env python3
    
    import argparse
    
    class ArgumentParserWithDefaults(argparse.ArgumentParser):
        def add_argument(self, *args, help=None, default=None, **kwargs):
            if help is not None:
                kwargs['help'] = help
            if default is not None and args[0] != '-h':
                kwargs['default'] = default
                if help is not None:
                    kwargs['help'] += ' Default: {}'.format(default)
            super().add_argument(*args, **kwargs)
    
    parser = ArgumentParserWithDefaults(
        formatter_class=argparse.RawTextHelpFormatter
    )
    parser.add_argument('-a', default=13, help='''my help
    for a''')
    parser.add_argument('-b', default=42, help='''my help
    for b''')
    parser.add_argument('--no-default', help='''my help
    for no-default''')
    parser.add_argument('--no-help', default=101)
    
    parser.print_help()
    print()
    print(parser.parse_args())
    

    Output:

    usage: main.py [-h] [-a A] [-b B] [--no-default NO_DEFAULT]
                   [--no-help NO_HELP]
    
    optional arguments:
      -h, --help            show this help message and exit
      -a A                  my help
                            for a Default: 13
      -b B                  my help
                            for b Default: 42
      --no-default NO_DEFAULT
                            my help
                            for no-default
      --no-help NO_HELP
    
    Namespace(a=13, b=42, no_default=None, no_help=101)
    

    ArgumentDefaultsHelpFormatter + RawTextHelpFormatter multiple inheritance

    Multiple inheritance just works, but it does not seem to be public API:

    #!/usr/bin/env python3
    
    import argparse
    
    class RawTextArgumentDefaultsHelpFormatter(
            argparse.ArgumentDefaultsHelpFormatter,
            argparse.RawTextHelpFormatter
        ):
            pass
    
    parser = argparse.ArgumentParser(
        formatter_class=RawTextArgumentDefaultsHelpFormatter
    )
    parser.add_argument('-a', default=13, help='''my help
    for a''')
    parser.add_argument('-b', default=42, help='''my help
    for b''')
    parser.print_help()
    

    Output:

    usage: a.py [-h] [-a A] [-b B]
    
    optional arguments:
      -h, --help  show this help message and exit
      -a A        my help
                  for a (default: 13)
      -b B        my help
                  for b (default: 42)
    

    It just works works because as we can see trivially from the sources https://github.com/python/cpython/blob/v3.6.5/Lib/argparse.py#L648 that:

    • RawTextHelpFormatter implements _split_lines
    • ArgumentDefaultsHelpFormatter implements _get_help_string

    so we can guess that they will work together just fine.

    However, this does not seem to be public API, and neither are the methods of formatter_class, so I don't think there is a public API way to do it currently. argparse docstring says:

    All other classes in this module are considered implementation details. (Also note that HelpFormatter and RawDescriptionHelpFormatter are only considered public as object names -- the API of the formatter objects is still considered an implementation detail.)

    See also: Customize argparse help message

    Tested on Python 3.6.5.

    0 讨论(0)
提交回复
热议问题