[英]where is the documentation for the python argparse helpformatter class?
I've found the documenation for the python argparse module, and it mentions the formatter_class. 我已经找到了python argparse模块的文档,并且提到了formatter_class。 I see nothing on that page for things like the width parameter or max_help_position.
我在该页面上看不到宽度参数或max_help_position之类的东西。 Where are those documented?
这些在哪里记录?
https://docs.python.org/3/library/argparse.html https://docs.python.org/3/library/argparse.html
Argparse uses a helper class argparse.helpformatter() which uses the max_help_position
and width
parameter (among others). Argparse使用辅助类argparse.helpformatter(),该类使用
max_help_position
和width
参数(以及其他参数)。 See this excellent answer that explains how it is used Explain lambda argparse.HelpFormatter(prog, width) . 看到这个出色的答案,它解释了如何使用解释lambda argparse.HelpFormatter(prog,width) 。
The problem you are having with finding documentation for it, is because the HelpFormatter is only public in the sense of its name. 查找有关文档的问题是因为HelpFormatter仅在其名称的意义上是公开的。 All its methods are private.
它的所有方法都是私有的。
This is taken from the source linked in the documentation you provided https://github.com/python/cpython/blob/2.7/Lib/argparse.py : 这取自您提供的文档https://github.com/python/cpython/blob/2.7/Lib/argparse.py中链接的源代码:
class HelpFormatter(object):
类HelpFormatter(object):
Formatter for generating usage messages and argument help strings.
用于生成使用情况消息和参数帮助字符串的格式化程序。
Only the name of this class is considered a public API.
仅此类的名称被认为是公共API。 All the methods provided by the class are considered an implementation detail.
该类提供的所有方法均被视为实现细节。
So the the argparse documentation itself is a mix of how-to and formal API description. 因此argparse文档本身是操作方法和正式API描述的组合。 Mostly it describes how to perform common parsing tasks.
通常,它描述了如何执行常见的解析任务。 Even though argparse consists of classes, the documentation does not formally describe the classes, and their subclassing and all methods.
尽管argparse由类组成,但文档并未正式描述这些类及其子类和所有方法。 It's not a reference API.
它不是参考API。
A workaround, would be to find another service that uses the HelpFormatter class that better document its variables, like this one from Discord https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.HelpFormatter . 一种解决方法是找到另一种使用HelpFormatter类的服务,该服务可以更好地记录其变量,例如Discord https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext中的服务.commands.HelpFormatter 。
Hope this helps. 希望这可以帮助。
Update 更新
Discord updated its links so the above link is now broken. Discord更新了其链接,因此上述链接现在已断开。 Find it in the WayBackMachine instead: https://web.archive.org/web/20180306073319/https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.HelpFormatter
而是在WayBackMachine中找到它: https ://web.archive.org/web/20180306073319/https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.HelpFormatter
The argparse
documentation more of a common usage manual than a formal module documentation. argparse
文档比正式的模块文档更多地是通用用法手册。 It doesn't, for example, list all (public) classes, and their methods. 例如,它没有列出所有(公共)类及其方法。 So for more custom uses you will have to look at the code, which fortunately is in just one file,
argparse.py
. 因此,对于更多自定义用途,您将必须查看代码,幸运的是,该代码位于一个文件
argparse.py
。
The help calling sequence is: 帮助调用顺序为:
parser.print_help
parser.format_help
parser._get_formatter
self.formatter_class(prog=self.prog)
In that use only the prog
parameter is set; 在这种情况下,仅设置
prog
参数; other values are default. 其他值为默认值。
class HelpFormatter(object):
def __init__(self,
prog,
indent_increment=2,
max_help_position=24,
width=None):
So these other parameters are available in the __init__
but not readily accessible to users. 因此,这些其他参数在
__init__
中可用,但用户不易访问。
Customizing the _get_formatter
method is one way of customizing these values. 自定义
_get_formatter
方法是自定义这些值的一种方法。 Another is to subclass HelpFormatter
. 另一个是将
HelpFormatter
子类HelpFormatter
。 It might also be possible to use partial
to set these values in the formatter_class
parameter. 也可能使用
partial
在formatter_class
参数中设置这些值。
I see @Magnus has found my earlier answer on this topic. 我看到@Magnus找到了关于该主题的较早答案。
So despite name, the formater_class
parameter does not have to be a class. 因此,尽管有名称,但
formater_class
参数不必是类。 In Python duck_typing, it just has to be something that _get_formatter
can use. 在Python duck_typing中,它必须是
_get_formatter
可以使用的东西。 It can be any function or lambda that takes prog
paramater. 它可以是任何需要
prog
参数的函数或lambda。
Drawing on the previous answer: 利用先前的答案:
f = lambda prog: argparse.HelpFormatter(prog, width=100)
f = functools.partial(argparse.HelpFormatter, width=100)
can both be used as: 都可以用作:
parser = argparse.ArgumentParser(formatter_class=f)
Let's see if I can illustrate how argparse
uses the formatter class. 让我们看看是否可以说明
argparse
如何使用formatter类。
print_usage
uses format_usage
( print_help
is similar but longer) 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()
With parser from a previous question: 使用上一个问题的解析器:
In [459]: p.print_usage()
usage: ipython3 [-h] [-f F] [-g [G [G ...]]] [-k [K [K ...]]]
I can replicate that with a direct call to the HelpFormatter
class: 我可以通过直接调用
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 ...]]]
If I create a formatter with a width
parameter I get some line wrapping: 如果我创建一个带有
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 ...]]]
The purpose of the suggested lambda
(and variations) is to replace the default formatter creation as in [460] with a custom one. 建议的
lambda
(和变体)的目的是用自定义代码替换[460]中的默认格式化程序创建。 The formatter_class
parameter lets us do that. formatter_class
参数使我们能够做到这一点。 It requires more Python knowledge than a simple width
parameter would, but ultimately gives us a lot more customizing power. 它比简单的
width
参数需要更多的Python知识,但最终为我们提供了更多的自定义功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.