Python argparse 和 bash 补全

发布于 2024-12-19 17:33:37 字数 311 浏览 2 评论 0原文

我希望在参数中也能自动完成我的 python 脚本。

我从来没有真正理解 bash_completion 是如何工作的(对于参数),但是在我深入研究之后,我明白了:

  1. 它使用“完整”将完成函数绑定到命令
  2. 每个完成函数基本上都是参数解析器的副本

第二点特别是不是很好,因为我希望它自动生成。

最好的事情是 shell 在每个选项卡上询问我的程序要完成什么,但我的印象是这实际上不起作用,对吗?

第二个选项可能只是编写一个从 argparse 解析器到正确完成的 shell 函数的转换器。

I would like to get auto-completion on my python scripts also in the arguments.

I had never really understood how the bash_completion worked (for arguments), but after I digged in I understood that:

  1. it uses "complete" to bind a completing function to a command
  2. every completing function basically is a copy of the argument parser

The second point in particular is not great, because I would like to have it automatically generated.

The best thing would be that the shell asks to my program at every TAB about what to complete, but I have the impression that this can't really work, is that correct?

The second option is probably just to write a converter from an argparse parser to a shell function which completes correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

萤火眠眠 2024-12-26 17:33:37

无耻的自我推销:https://github.com/kislyuk/argcomplete

argcomplete提供了bash(和zsh) ) argparse 的完成。

Shameless self-promotion: https://github.com/kislyuk/argcomplete

argcomplete provides bash (and zsh) completion for argparse.

孤千羽 2024-12-26 17:33:37

Bash “完成” 确实很棒。对于用 Python 编写的程序来说很容易......

我认为这正是你想要的: optcomplete: Shell Completion Self-Generator对于Python。例如,它可以作为 Ubuntu 中的“python-optcomplete”包使用。

您在 python 程序中插入几行,然后用户(一次)运行 bash“完成”程序来告诉 bash 如何完成参数:

complete -F _optcomplete <program>

现在用户已经完成了!默认情况下,它会简单地完成程序选项。请参阅示例,了解如何自定义特定选项的完成方式。它编写精美,并且易于扩展以处理子命令、备用完成选项等!

更新:

要在 zsh 中完成(对于 optparse 和 argparse),请参阅 genzshcomp 0.3。 1:Python 包索引

正如 @englebip 所指出的,对于 Python 2.7 中引入的新 argparse 模块,我们仍然需要类似的东西3.2,因为 optparse 现已弃用。

以下是关于朝着这个方向前进的讨论:

另请参阅有关如何完成的背景:argparse(和已弃用的 optparse)如何响应在 bash 中,在 python 程序名称之后按“tab”键? - 堆栈溢出

Bash "completion" really is great. And easy for programs written in Python....

I think this is just what you want: optcomplete: Shell Completion Self-Generator for Python. It is available, e.g., as the "python-optcomplete" package in Ubuntu.

You insert a few lines in your python program, and the user (one time) runs the bash "complete" program to tell bash how to complete the arguments:

complete -F _optcomplete <program>

and now the user has completion! By default it gives simple completion on program options. See the example for how to customize how completion works for a particular option. It is beautifully written, and easy to extend to handle sub-commands, alternate completion options, etc.!

Update:

For completion in zsh (for both optparse and argparse) see genzshcomp 0.3.1 : Python Package Index

As noted by @englebip, we still need something similar for the new argparse module, introduced in Python 2.7 and 3.2, since optparse is now deprecated.

Here is the discussion on moving in that direction:

See also this background on how it is done: How does argparse (and the deprecated optparse) respond to 'tab' keypress after python program name, in bash? - Stack Overflow

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