代码之家  ›  专栏  ›  技术社区  ›  kdubs

python argparse helpformatter类的文档在哪里?

  •  1
  • kdubs  · 技术社区  · 7 年前

    我找到了python argparse模块的文档,其中提到了formatter\u类。我在那页上没有看到宽度参数或max\u help\u位置之类的内容。这些记录在哪里?

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

    2 回复  |  直到 7 年前
        1
  •  2
  •   Community CDub    4 年前

    Argparse使用助手类Argparse。helpformatter(),它使用 max_help_position width 参数(除其他外)。看看这个解释如何使用它的优秀答案 Explain lambda argparse.HelpFormatter(prog, width)

    您在查找文档时遇到的问题是,HelpFormatter仅在名称意义上是公共的。它的所有方法都是私有的。

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

    类帮助格式化程序(对象):

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

    只有此类的名称才被视为公共API。所有方法 类提供的信息被视为实现细节。

    因此,argparse文档本身是how-to和正式API描述的混合体。它主要描述如何执行常见的解析任务。尽管argparse由类组成,但文档并没有正式描述这些类及其子类化和所有方法。它不是一个参考API。

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

    希望这有帮助。

    使现代化

    Discord更新了其链接,因此上述链接现在已断开。改为在Wayback机器中查找: https://web.archive.org/web/20180306073319/https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.HelpFormatter

        2
  •  1
  •   hpaulj    7 年前

    这个 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 。也可以使用 partial 在中设置这些值 formatter_class 参数

    我知道@Magnus找到了我之前关于这个话题的答案。

    所以不管名字如何 formater_class 参数不必是类。在Python duck\u类型中,它必须是 _get\u格式化程序 可以用。它可以是任何函数或lambda prog程序 参数。

    根据前面的回答:

    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_usage ( print_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 ...]]]
    

    我可以通过直接呼叫 帮助格式化程序 类别:

    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 参数I得到一些换行:

    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\u类 参数允许我们这样做。它需要比简单的 宽度 参数会,但最终会给我们更多的定制功能。