简体   繁体   English

以编程方式获取bash完成选项

[英]getting bash completion options programmatically

I want a function that programmatically returns completion options from either bash or zsh. 我想要一个以编程方式从bash或zsh返回完成选项的函数。 There are lots of examples of related questions on stackoverflow but no proper, generic answers anywhere. 有很多关于stackoverflow 的相关问题示例,但是在任何地方都没有适当的通用答案。 I do NOT want to know how to write a specific completer function for bash. 我不想知道如何为bash编写特定的完成函数。

I've already tried implementing this by reading debian /etc/completion shell code, by echoing control-codes for tab into "bash -i", and even tried using automated subprocess interaction with python-pexpect. 我已经尝试通过阅读debian / etc / completion shell代码,通过将tab的控制代码回显到“ bash -i”中来实现这一目标,甚至尝试使用与python-pexpect的自动子流程交互。 Every time I thought I was successful, I find some small problem that invalidates the whole solution. 每当我以为自己成功了,就会发现一些小问题,使整个解决方案无效。 I'd accept a solution in any language , but ideally it would be python. 我会接受任何语言的解决方案 ,但理想情况下是python。 Obviously the exact input output would vary depending on systems, but take a look at the example I/O below: 显然,确切的输入输出会因系统而异,但请看下面的示例I / O:

  • function("git lo") returns ["log","lol","lola"] function(“ git lo”)返回[“ log”,“ lol”,“ lola”]
  • function("apt-get inst") returns ["apt-get install"] function(“ apt-get inst”)返回[“ apt-get install”]
  • function("apt-get") returns [] function(“ apt-get”)返回[]
  • function("apt-get ") returns ["apt-get autoclean","apt-get autoremove", ...] function(“ apt-get”)返回[“ apt-get autoclean”,“ apt-get autoremove”,...]
  • function ("./setup") returns ["./setup.py"] 函数(“ ./setup”)返回[“ ./setup.py”]

If you are thinking of a solution written in shell, it would ideally be something I can execute without "source"ing. 如果您正在考虑用shell编写的解决方案,那么理想情况下,我可以在不进行“源代码化”的情况下执行该解决方案。 For instance bash "compgen" command looks interesting (try "compgen -F _git"), but note that "bash -c 'compgen -F _git'" does not work because the completion helper "_git" is not in scope. 例如,bash“ compgen”命令看起来很有趣(尝试使用“ compgen -F _git”),但是请注意,“ bash -c'compgen -F _git'”不起作用,因为补全助手“ _git”不在范围内。

This gist is my best solution so far. 到目前为止, 是我最好的解决方案。 It meets all the requirements, works well for multiple versions of bash on multiple OS's but it requires a subprocess call and it's so complicated it's absurd. 它满足所有要求,可以在多个OS上的多个版本的bash上很好地工作,但是它需要一个子进程调用,而且它是如此复杂以至于荒唐。 The comments includes full documentation of all the outrageous slings and arrows. 评论包括所有令人发指的吊索和箭头的完整文档。 I'm still hoping for something more reasonable to come along, but unless it does.. this is it! 我仍然希望能有更合理的选择,但除非如此,否则!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM