【问题标题】:How to reduce indentation level of argument help in argparse?如何减少argparse中参数帮助的缩进级别?
【发布时间】:2018-03-15 05:03:56
【问题描述】:

我正在使用 Python 的 argparse,并且我希望参数帮助文本的缩进更少。这是argparse 正在生成的:

$ ./help.py -h
usage: help.py [-h] [--program-argument PROGRAM_ARGUMENT]

Description of program

optional arguments:
  -h, --help            show this help message and exit
  --program-argument PROGRAM_ARGUMENT
                        This is some help text about --program-argument. For example:

                            --program-argment "You can supply a string as the program argument"

我希望它生成更像这样的东西:

$ ./help.py -h
usage: help.py [-h] [--program-argument PROGRAM_ARGUMENT]

Description of program

optional arguments:
  -h, --help            show this help message and exit
  --program-argument PROGRAM_ARGUMENT
      This is some help text about --program-argument. For example:

          --program-argment "You can supply a string as the program argument"

这可以实现吗?这是我的代码:

#! /usr/bin/env python
import argparse

HELP_TEXT = """\
This is some help text about --program-argument. For example:

    --program-argment "You can supply a string as the program argument"
"""


if __name__ == '__main__':
    argument_parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter,
        description=('Description of program'))
    argument_parser.add_argument(
        '--program-argument',
        help=HELP_TEXT
    )
    args, unknown = argument_parser.parse_known_args()

【问题讨论】:

    标签: python argparse


    【解决方案1】:

    argparse 格式化程序支持几个可以帮助控制某些格式化的初始化值。它们都派生自HelpFormatter,它有这个__init__ 方法。

    class HelpFormatter(object):
        """Formatter for generating usage messages and argument help strings.
    
        Only the name of this class is considered a public API. All the methods
        provided by the class are considered an implementation detail.
        """
    
        def __init__(self,
                     prog,
                     indent_increment=2,
                     max_help_position=24,
                     width=None):
        # stuff
    

    max_help_position 用于确定帮助子消息的缩进程度,因此您可以尝试将其缩减为 1012 之类的内容,以减少消息的缩进。

    #!/usr/bin/env python
    import argparse
    
    HELP_TEXT = """\
    This is some help text about --program-argument. For example:
    
        --program-argment "You can supply a string as the program argument"
    """
    
    less_indent_formatter = lambda prog: argparse.RawTextHelpFormatter(prog, max_help_position=10)
    
    
    if __name__ == '__main__':
        argument_parser = argparse.ArgumentParser(
            formatter_class=less_indent_formatter,
            description=('Description of program'))
        argument_parser.add_argument(
            '--program-argument',
            help=HELP_TEXT
        )
        args, unknown = argument_parser.parse_known_args()
    

    这会导致:

    usage: help.py [-h] [--program-argument PROGRAM_ARGUMENT]
    
    Description of program
    
    optional arguments:
      -h, --help
              show this help message and exit
      --program-argument PROGRAM_ARGUMENT
              This is some help text about --program-argument. For example:
    
                  --program-argment "You can supply a string as the program argument"
    

    6 的值如下所示:

    usage: help.py [-h] [--program-argument PROGRAM_ARGUMENT]
    
    Description of program
    
    optional arguments:
      -h, --help
          show this help message and exit
      --program-argument PROGRAM_ARGUMENT
          This is some help text about --program-argument. For example:
    
              --program-argment "You can supply a string as the program argument"
    

    【讨论】:

    • 为什么less_indent_formatter 格式化程序必须是lambda?我们的 linter 抱怨了这一点,所以我改成了 def,但得到了像 length of metavar tuple does not match nargs 这样的错误。
    • @Drew lambda 是最方便的,一种方法也可以。如果您遇到length of metavar 问题,请确保该方法的参数称为prog,因为argparse 会这样调用它。如果您需要进一步的帮助,请告诉我 - 当我回到家并可以使用电脑时,我会回复您。
    • 啊,是的,我已将参数名称更改为 program。没有意识到它是通过 kwarg 传递的。
    猜你喜欢
    • 2021-04-04
    • 2022-01-04
    • 1970-01-01
    • 2020-10-12
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 2021-08-13
    相关资源
    最近更新 更多