对论证的适当帮助
当脚本使用是这样的时候,Python optparse 工作得很好
%prog [options] [args]
但是我需要为带有 1 个必需参数的脚本编写帮助,所以用法将像这样
%prog action [options] [args]
当你使用 Subversion 时你可以看到类似的东西 - 它的使用字符串是
svn <subcommand> [options] [args]
所以我的问题是:是否可以以 Subversion 的方式使用 optparse 为所需参数准备帮助?结果我想看到这样的帮助:
Usage: python myscript.py action [options] [args]
Available actions:
foo
bar
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Verbose mode. Output debug log to stdout.
Python optparse works very good when script usage is something like this
%prog [options] [args]
But I need to write help for script with 1 required argument, so usage will be like this
%prog action [options] [args]
You can see something similar when you use Subversion - its usage string is
svn <subcommand> [options] [args]
So my question is: is it possible to prepare help for required argument with optparse in the manner of Subversion? As a result I want to see help like this:
Usage: python myscript.py action [options] [args]
Available actions:
foo
bar
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Verbose mode. Output debug log to stdout.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为对您来说一个很好的解决方案是 argparse,它已建议包含在 Python 2.7 和 3.2 中。我相信它可以处理子命令,并且链接的页面包含一个指向从 optparse 移植代码的页面的链接。
另请参阅问题 command-line-arguments-in-python,有人编辑了该问题似乎包含与您想要的完全相同的内容的参考列表:
I think a good solution for you is argparse, which has been proposed for inclusion in Python 2.7 and 3.2. It handles subcommands, I believe as you want, and the linked page includes a link to a page on porting your code from optparse.
See also the question command-line-arguments-in-python, into which someone edited a list of references that appears to include exactly the same thing you want:
是的。您可以像这样设置用法字符串:
打印以下内容:
这是几乎逐字复制自 文档。
编辑:
根据您的评论,您可以使用描述来实现类似的功能,但不能在其中添加换行符。
看起来像这样:
Yes. You can set the usage string like this:
Prints the following:
This was copied almost verbatim from the docs.
Edit:
Based on your comment you could use the description to achieve something similar, though you can't put new-line characters in it.
Will look like this:
我也遇到了这个问题。我的解决方案是在列表或元组中声明命令,将它们格式化为 OptionParser 的
usage
参数,然后使用解析器提供的参数列表来确定是否提供了命令,因为从技术上讲它必须是args[0]
。例如:这会给你一个看起来像 Subversion 的命令系统,但不可否认 optparse 可能会更好。我听说 argparse 模块应该将其纳入 stdlib,但由于 2.7 是 2 系列版本中的最后一个,我想您必须等待它被合并到 3 系列中.x。当然,您可以只安装 argparse,但在某些情况下这会很麻烦。
I've run into this problem as well. My solution was to declare commands in a list or tuple, format them into the
usage
parameter of OptionParser and then use the args list provided by the parser to determine if a command was provided or not, since it technically has to beargs[0]
. Eg:This kinda gets you a command system that looks like Subversion's, but admittedly optparse could be better. I've heard the argparse module is supposed to make it into the stdlib, but with 2.7 being the last of the 2 series releases, I guess you'd have to wait for it to be incorporated into 3.x. Of course you can just install argparse, but that's a drag in some cases.