对论证的适当帮助

发布于 2024-08-14 07:04:15 字数 673 浏览 3 评论 0原文

当脚本使用是这样的时候,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的黑色迷你裙 2024-08-21 07:04:15

我认为对您来说一个很好的解决方案是 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:

逆夏时光 2024-08-21 07:04:15

是的。您可以像这样设置用法字符串:

usage = "%prog action [options] [args]"
parser = OptionParser(usage=usage)
parser.add_option("-v", "--verbose",
                  action="store_true", dest="verbose", default=True,
                  help="make lots of noise [default]")

打印以下内容:

Usage:  action [options] [args]

Options:
  -h, --help     show this help message and exit
  -v, --verbose  make lots of noise [default]

这是几乎逐字复制自 文档

编辑:

根据您的评论,您可以使用描述来实现类似的功能,但不能在其中添加换行符。

parser.description = 'Available actions: foo, bar'

看起来像这样:

Usage:  action [options] [args]

Available actions: foo, bar

Options:
  -h, --help     show this help message and exit
  -v, --verbose  make lots of noise [default]

Yes. You can set the usage string like this:

usage = "%prog action [options] [args]"
parser = OptionParser(usage=usage)
parser.add_option("-v", "--verbose",
                  action="store_true", dest="verbose", default=True,
                  help="make lots of noise [default]")

Prints the following:

Usage:  action [options] [args]

Options:
  -h, --help     show this help message and exit
  -v, --verbose  make lots of noise [default]

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.

parser.description = 'Available actions: foo, bar'

Will look like this:

Usage:  action [options] [args]

Available actions: foo, bar

Options:
  -h, --help     show this help message and exit
  -v, --verbose  make lots of noise [default]
嗼ふ静 2024-08-21 07:04:15

我也遇到了这个问题。我的解决方案是在列表或元组中声明命令,将它们格式化为 OptionParser 的 usage 参数,然后使用解析器提供的参数列表来确定是否提供了命令,因为从技术上讲它必须是args[0]。例如:

self.commands = ('foo', 'bar' ...)
self.parser = <initialized instance of OptionParser>
(self.options, self.args) = parser.parse_args()

if len(self.args) == 0:
   self.parser.error("Command required")

self.command = self.args[0]
if not self.command in self.commands:
   self.parser.error("Command not recognized")

#... etc

这会给你一个看起来像 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 be args[0]. Eg:

self.commands = ('foo', 'bar' ...)
self.parser = <initialized instance of OptionParser>
(self.options, self.args) = parser.parse_args()

if len(self.args) == 0:
   self.parser.error("Command required")

self.command = self.args[0]
if not self.command in self.commands:
   self.parser.error("Command not recognized")

#... etc

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文