繁体   English   中英

python argparse helpformatter类的文档在哪里?

[英]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使用辅助类argparse.helpformatter(),该类使用max_help_positionwidth参数(以及其他参数)。 看到这个出色的答案,它解释了如何使用解释lambda argparse.HelpFormatter(prog,width)

查找有关文档的问题是因为HelpFormatter仅在其名称的意义上是公开的。 它的所有方法都是私有的。

这取自您提供的文档https://github.com/python/cpython/blob/2.7/Lib/argparse.py中链接的源代码:

类HelpFormatter(object):

用于生成使用情况消息和参数帮助字符串的格式化程序。

仅此类的名称被认为是公共API。 该类提供的所有方法均被视为实现细节。

因此argparse文档本身是操作方法和正式API描述的组合。 通常,它描述了如何执行常见的解析任务。 尽管argparse由类组成,但文档并未正式描述这些类及其子类和所有方法。 它不是参考API。

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

希望这可以帮助。

更新

Discord更新了其链接,因此上述链接现在已断开。 而是在WayBackMachine中找到它: https ://web.archive.org/web/20180306073319/https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.HelpFormatter

argparse文档比正式的模块文档更多地是通用用法手册。 例如,它没有列出所有(公共)类及其方法。 因此,对于更多自定义用途,您将必须查看代码,幸运的是,该代码位于一个文件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方法是自定义这些值的一种方法。 另一个是将HelpFormatter子类HelpFormatter 也可能使用partialformatter_class参数中设置这些值。

我看到@Magnus找到了关于该主题的较早答案。

因此,尽管有名称,但formater_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如何使用formatter类。

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类来复制它:

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参数的格式化程序, 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知识,但最终为我们提供了更多的自定义功能。

暂无
暂无

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

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