How to reduce indentation level of argument help in argparse?(如何在argparse中降低参数帮助的缩进级别?)
本文介绍了如何在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
格式化程序支持多个初始化值,有助于控制某些格式。它们都派生自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
用于确定帮助子邮件的缩进程度,因此您可以尝试将其缩减为类似10
或12
的缩进,以减少邮件的缩进。
#!/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"
这篇关于如何在argparse中降低参数帮助的缩进级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何在argparse中降低参数帮助的缩进级别?


猜你喜欢
- 我如何卸载 PyTorch? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01