python argparse helpformatter class 的文档在哪里?

where is the documentation for the python argparse helpformatter class?

我找到了 python argparse 模块的文档,它提到了 formatter_class。我在该页面上看不到宽度参数或 max_help_position 之类的内容。这些记录在哪里?

https://docs.python.org/3/library/argparse.html

Argparse 使用辅助程序 class argparse.helpformatter(),它使用 max_help_positionwidth 参数(以及其他参数)。请参阅解释如何使用它的优秀答案

您在查找它的文档时遇到的问题是因为 HelpFormatter 只是 public 就其名称而言。它的所有方法都是私有的。

这取自 link 在您提供的文档中 https://github.com/python/cpython/blob/2.7/Lib/argparse.py:

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.

所以 argparse 文档本身是 how-to 和正式 API 描述的混合体。它主要描述了如何执行常见的解析任务。尽管 argparse 由 classes 组成,文档并未正式描述 classes 及其子 classing 和所有方法。这不是参考 API.

一个解决方法是找到另一个使用 HelpFormatter class 的服务,更好地记录其变量,例如来自 Discord https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.HelpFormatter.

的服务

希望对您有所帮助。

更新

Discord 更新了它的 links,所以上面的 link 现在被破坏了。改为在 WayBackMachine 中找到它:https://web.archive.org/web/20180306073319/https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.HelpFormatter

argparse 文档比正式的模块文档更像是一个常用的使用手册。例如,它没有列出所有 (public) classes 及其方法。因此,对于更多自定义用途,您将不得不查看代码,幸运的是它只在一个文件中,argparse.py.

帮助调用顺序为:

parser.print_help
   parser.format_help
       parser._get_formatter
           self.formatter_class(prog=self.prog)

在那个使用中只设置了prog参数;其他值为默认值。

class HelpFormatter(object):
         def __init__(self,
             prog,
             indent_increment=2,
             max_help_position=24,
             width=None):

所以这些其他参数在 __init__ 中可用,但用户不容易访问。

自定义 _get_formatter 方法是自定义这些值的一种方式。另一个是subclassHelpFormatter。也可以使用 partialformatter_class 参数中设置这些值。

我看到@Magnus 已经找到了我之前关于这个主题的答案。

因此,尽管有名称,formater_class 参数不必是 class。在 Python duck_typing 中,它必须是 _get_formatter 可以使用的东西。它可以是采用 prog 参数的任何函数或 lambda。

借鉴之前的答案:

f = lambda prog: argparse.HelpFormatter(prog, width=100)
f = functools.partial(argparse.HelpFormatter, width=100)

都可以用作:

parser = argparse.ArgumentParser(formatter_class=f)

(插图)

让我们看看我能否说明 argparse 如何使用格式化程序 class。

print_usage 使用 format_usageprint_help 类似但更长)

def format_usage(self):
    formatter = self._get_formatter()
    formatter.add_usage(self.usage, self._actions,
                        self._mutually_exclusive_groups)
    return formatter.format_help()

使用上一个问题的解析器:

In [459]: p.print_usage()
usage: ipython3 [-h] [-f F] [-g [G [G ...]]] [-k [K [K ...]]]

我可以通过直接调用 HelpFormatter class:

来复制它
In [460]: f = argparse.HelpFormatter(prog='foo')
In [461]: f.add_usage(p.usage, p._actions,p._mutually_exclusive_groups)
In [462]: print(f.format_help())
usage: foo [-h] [-f F] [-g [G [G ...]]] [-k [K [K ...]]]

如果我创建一个带有 width 参数的格式化程序,我会得到一些换行:

In [463]: f = argparse.HelpFormatter(prog='foo',width=40)
In [464]: f.add_usage(p.usage, p._actions,p._mutually_exclusive_groups)
In [465]: print(f.format_help())
usage: foo [-h] [-f F] [-g [G [G ...]]]
           [-k [K [K ...]]]

建议的 lambda(和变体)的目的是将 [460] 中的默认格式化程序创建替换为自定义格式化程序。 formatter_class 参数让我们可以做到这一点。它需要比简单的 width 参数更多的 Python 知识,但最终给了我们更多的定制能力。