gpt4 book ai didi

带有换行的 Python argparse.RawTextHelpFormatter

转载 作者:行者123 更新时间:2023-11-28 20:40:25 24 4
gpt4 key购买 nike

在我的参数解析代码中,我需要使用 argparse.RawTextHelpFormatter,但我也希望以与默认格式化程序相同的方式自动以固定宽度换行。

有什么优雅的方法可以将这两种行为结合起来吗?

最佳答案

编写自定义 RawTextHelpFormatter

您可以编写自己的 RawTextHelpFormatterRawTextHelpFormatterArgumentDefaultsHelpFormatter 仅在方法 _fill_text_split_lines 上存在差异,因此只需覆盖 _spilt_lines_ 方法通过换行解决了这个问题。

import argparse
import textwrap as _textwrap

class LineWrapRawTextHelpFormatter(argparse.RawDescriptionHelpFormatter):
def _split_lines(self, text, width):
text = self._whitespace_matcher.sub(' ', text).strip()
return _textwrap.wrap(text, width)


parser = argparse.ArgumentParser(
prog='PROG',
formatter_class=LineWrapRawTextHelpFormatter)
parser.add_argument('--foo', type=int, default=42, help="FOO! Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an u")
parser.add_argument('bar', nargs='*', default=[1, 2, 3], help="BAR! FOO! Lorem Ipsum is simply dummy text of the printing and typesetting industry.")
parser.print_help()

输出

usage: PROG [-h] [--foo FOO] [bar [bar ...]]

positional arguments:
bar BAR! FOO! Lorem Ipsum is simply dummy text of the printing and
typesetting industry.

optional arguments:
-h, --help show this help message and exit
--foo FOO FOO! Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industry's
standard dummy text ever since the 1500s, when an u

如您所见,该行会自动换行。如果要调整宽度,可以在 _textwrap.wrap(text, width) 处对宽度进行硬编码(它只是以 FOO!Lorem 开头的部分的宽度)在 _spilit_lines 方法中或使用 _os.environ['COLUMNS'](这是完整帮助文本的宽度)。

columns=40 的代码

import os
os.environ['COLUMNS'] = "40"

输出

usage: PROG [-h] [--foo FOO]
[bar [bar ...]]

positional arguments:
bar BAR! FOO! Lorem Ipsum is
simply dummy text of the
printing and typesetting
industry.

optional arguments:
-h, --help show this help message
and exit
--foo FOO FOO! Lorem Ipsum is
simply dummy text of the
printing and typesetting
industry. Lorem Ipsum
has been the industry's
standard dummy text ever
since the 1500s, when an
u

硬编码 40

def _split_lines(self, text, width):
text = self._whitespace_matcher.sub(' ', text).strip()
return _textwrap.wrap(text, 40)

输出

usage: PROG [-h] [--foo FOO] [bar [bar ...]]

positional arguments:
bar BAR! FOO! Lorem Ipsum is simply dummy
text of the printing and typesetting
industry.

optional arguments:
-h, --help show this help message and exit
--foo FOO FOO! Lorem Ipsum is simply dummy text of
the printing and typesetting industry.
Lorem Ipsum has been the industry's
standard dummy text ever since the

PreserveWhiteSpaces 和例如要点

如果你想保留换行符前的空白,我只写了一个 PreserveWhiteSpaceWrapRawTextHelpFormatter。

import argparse
import textwrap as _textwrap
import re

class PreserveWhiteSpaceWrapRawTextHelpFormatter(argparse.RawDescriptionHelpFormatter):
def __add_whitespace(self, idx, iWSpace, text):
if idx is 0:
return text
return (" " * iWSpace) + text

def _split_lines(self, text, width):
textRows = text.splitlines()
for idx,line in enumerate(textRows):
search = re.search('\s*[0-9\-]{0,}\.?\s*', line)
if line.strip() is "":
textRows[idx] = " "
elif search:
lWSpace = search.end()
lines = [self.__add_whitespace(i,lWSpace,x) for i,x in enumerate(_textwrap.wrap(line, width))]
textRows[idx] = lines

return [item for sublist in textRows for item in sublist]

它只是查看文本文本的缩进是什么,并为每个 _textwrap.warp 行添加它。使用此参数调用。

parser = argparse.ArgumentParser(
prog='PROG',
formatter_class=PreserveWhiteSpaceWrapRawTextHelpFormatter)
parser.add_argument('--foo', type=int, default=42, help="""Just Normal Bullet Point with Some Enter in there

1. Lorem Ipsum has been the industry's standard dummy text ever since
2. the 1500s, when an u
3. Lorem Ipsum is simply dummy text of the printing and typesetting industry

Some other Bullet POint

- Ipsum is simply dummy text of the printing and typesetting industry
- Ipsum is simply dummy text of the printing and typesetting industry

And No BulletPoint
Ipsum is simply dummy text of the printing and typesetting industry
Ipsum is simply dummy text of the printing and typesetting industry
""")
parser.add_argument('bar', nargs='*', default=[1, 2, 3], help="BAR! FOO! Lorem Ipsum is simply dummy text of the printing and typesetting industry.")
parser.print_help()

输出

usage: PROG [-h] [--foo FOO] [bar [bar ...]]

positional arguments:
bar BAR! FOO! Lorem Ipsum is simply dummy text of the printing and
typesetting industry.

optional arguments:
-h, --help show this help message and exit
--foo FOO Just Normal Bullet Point with Some Enter in there

1. Lorem Ipsum has been the industry's standard dummy text
ever since
2. the 1500s, when an u
3. Lorem Ipsum is simply dummy text of the printing and
typesetting industry

Some other Bullet POint

- Ipsum is simply dummy text of the printing and typesetting
industry
- Ipsum is simply dummy text of the printing and typesetting
industry

And No BulletPoint and no Enter
Ipsum is simply dummy text of the printing and typesetting
industry
Ipsum is simply dummy text of the printing and typesetting
industry

关于带有换行的 Python argparse.RawTextHelpFormatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35917547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com