Python argparse 和 bash 补全
我希望在参数中也能自动完成我的 python 脚本。
我从来没有真正理解 bash_completion 是如何工作的(对于参数),但是在我深入研究之后,我明白了:
- 它使用“完整”将完成函数绑定到命令
- 每个完成函数基本上都是参数解析器的副本
第二点特别是不是很好,因为我希望它自动生成。
最好的事情是 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:
- it uses "complete" to bind a completing function to a command
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无耻的自我推销: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.
Bash “完成” 确实很棒。对于用 Python 编写的程序来说很容易......
我认为这正是你想要的: optcomplete: Shell Completion Self-Generator对于Python。例如,它可以作为 Ubuntu 中的“python-optcomplete”包使用。
您在 python 程序中插入几行,然后用户(一次)运行 bash“完成”程序来告诉 bash 如何完成参数:
现在用户已经完成了!默认情况下,它会简单地完成程序选项。请参阅示例,了解如何自定义特定选项的完成方式。它编写精美,并且易于扩展以处理子命令、备用完成选项等!
更新:
要在 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:
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, sinceoptparse
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