带换行的Python argparse.RawTextHelpFormatter
在我的参数解析代码中,我需要使用带换行的Python argparse.RawTextHelpFormatter,python,argparse,Python,Argparse,在我的参数解析代码中,我需要使用argparse.RawTextHelpFormatter,但我还希望以与默认格式化程序相同的方式,以固定宽度自动包装行 有什么优雅的方法可以将这两种行为结合起来吗?编写自定义RawTextHelpFormatter 您只需编写自己的RawTextHelpFormatter。RawTextHelpFormatter与ArgumentDefaultsHelpFormatter中的\u fill\u text和\u split\u line方法只有不同之处,因此只需覆
argparse.RawTextHelpFormatter
,但我还希望以与默认格式化程序相同的方式,以固定宽度自动包装行
有什么优雅的方法可以将这两种行为结合起来吗?编写自定义RawTextHelpFormatter
您只需编写自己的RawTextHelpFormatter
。RawTextHelpFormatter
与ArgumentDefaultsHelpFormatter
中的\u fill\u text
和\u split\u line
方法只有不同之处,因此只需覆盖\u split\u line
方法即可通过换行解决此问题
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()
输出
如您所见,该行将自动换行。如果要调整宽度,可以在\u textwrap.wrap(text,width)
中对宽度进行硬编码(它仅是以FOO!Lorem
开头的部分的宽度),使用\u spilit\u lines
方法或使用\u os.environ['COLUMNS]
(这是完整帮助文本的宽度)
带列代码=40
import os
os.environ['COLUMNS'] = "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 1500s, when an
u
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
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
带有硬代码40的代码
import os
os.environ['COLUMNS'] = "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 1500s, when an
u
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
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
保留空格和例如项目符号点
如果您想保留换行符前面的空白,我只编写了一个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]
它只是查看文本的缩进,并为每个\u 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 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
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
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
也许
textwrpa
可以帮助您吗?:听起来您需要自己的自定义格式化程序。查看RawTextHelpFormatter
的代码。请注意它修改的方法。查看常规帮助格式化程序中使用该方法的位置argparse
导入textwrap
,并使用它来拆分行RawTextHelpFormatter
更改split_行
,绕过textwrap
。也许你可以实施部分旁路。是一个错误/问题,它提出了一个混合格式化程序,一个允许要点和段落等特性的程序,但也允许包装。它可能不会添加到argparse
,但建议的更改可以添加到您的代码中。我的印象是OP希望保留自己的新行,同时仍然允许包装长行。例如,使用要点或段落。我不认为自定义宽度是主要考虑因素。但我可能错了。@hpaulj我认为你的通知和评论是对的。添加了我编写的保留空白wraprawtexthelpformatter
的部分。太好了,非常感谢!通过一些小的修改(增强),这完全符合预期,甚至更好。@hpaulj是对的,主要的问题是在自动换行的同时保留我自己的格式。在Python 3.5.2中,如果您不使用字符[
和],那么使用/帮助折叠到$COLUMNS
似乎是自动实现的,而不需要上述任何上瘾
如果你不想得到一个讨厌的bug。