繁体   English   中英

如何在argparse中降低参数帮助的缩进级别?

[英]How to reduce indentation level of argument help in argparse?

我正在使用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()

argparse格式化程序支持多个初始化值,这些值可以帮助控制某些格式化。 它们全部来自具有此__init__方法的HelpFormatter

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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM