如何使用 Python 的 optparse 格式化位置参数帮助?
正如文档中提到的optparse.OptionParser
使用 IndentedHelpFormatter
输出格式化的选项帮助,为此我找到了一些API 文档。
我想在使用文本中显示所需位置参数的类似格式的帮助文本。 是否有适配器或简单的使用模式可用于类似的位置参数格式?
说明
最好仅使用 stdlib。 Optparse 做得很好,除了这个格式上的细微差别,我觉得我们应该能够在不导入整个其他包的情况下修复它。 :-)
As mentioned in the docs the optparse.OptionParser
uses an IndentedHelpFormatter
to output the formatted option help, for which which I found some API documentation.
I want to display a similarly formatted help text for the required, positional arguments in the usage text. Is there an adapter or a simple usage pattern that can be used for similar positional argument formatting?
Clarification
Preferably only using the stdlib. Optparse does great except for this one formatting nuance, which I feel like we should be able to fix without importing whole other packages. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好的选择是为 optparse 模块编写补丁。 同时,您可以通过稍微修改的 OptionParser 类来完成此操作。 这并不完美,但它会完成你想要的事情。
运行此命令得到的输出:
The best bet would be to write a patch to the optparse module. In the meantime, you can accomplish this with a slightly modified OptionParser class. This isn't perfect, but it'll get what you want done.
And the output you get from running this:
尝试查看 argparse。 文档称它支持位置参数和更美观的帮助消息。
Try taking a look at argparse. Documentation says it supports position arguments and nicer looking help messages.
我对一个干净的解决方案感兴趣; 我想不出一个。 OptionParser 实际上完全专注于选项; 据我所知,它没有给你任何与位置参数相关的信息。
我所做的是为每个位置参数生成一个小文档块列表,使用
\t
来获得正确的间距。 然后我用换行符将它们连接起来,并将其附加到传递给 OptionParser 的“usage”字符串中。它看起来不错,但感觉很愚蠢,当然该文档最终出现在选项列表上方。 我还没有找到解决这个问题的任何方法,或者如何做任何复杂的事情,即在位置参数的描述下方描述了一组给定的选项,因为它们仅适用于该参数。
我查看了猴子修补 OptionParser 的方法,我记得(这是大约一年前)它不会那么困难,但我不想走那条路。
I'd be interested in a clean solution to this; I wasn't able to come up with one. The OptionParser really focuses entirely on the options; it doesn't give you anything to work with position args, as far as I've been able to find.
What I did was to generate a list of little documentation blocks for each of my positional arguments, using
\t
s to get the right spacing. Then I joined them with newlines, and appended that to the 'usage' string that gets passed to the OptionParser.It looks fine, but it feels silly, and of course that documentation ends up appearing above the list of options. I haven't found any way around that, or how to do any complex stuff, i.e. a given set of options is described beneath the description for a positional arg, because they only apply to that arg.
I looked at monkey-patching OptionParser's methods and I remember (this was a year or so ago) that it wouldn't have been that difficult, but I didn't want to go down that path.
大多数位置参数的帮助文本类似于 *NIX 框手册页中常用的格式。 查看如何记录“cp”命令。 您的帮助文本应该与此类似。
否则,只要您在使用解析器时填写“help”参数,文档就应该自行生成。
Most help text for positional arguments resembles the format frequently used in man pages for *NIX boxes. Take a look at how the 'cp' command is documented. Your help text should resemble that.
Otherwise as long as you fill out the "help" argument while using the parser, the documentation should produce itself.