如何从我自己的脚本中提供制表符补全来捕获shell? [英] How can I provide tab completions to fish shell from my own script?

查看:93
本文介绍了如何从我自己的脚本中提供制表符补全来捕获shell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Ubuntu 13.10和fish 2.1.0.我想为自己编写一个Python脚本,以从命令行执行一些任务.该脚本将需要命令行参数.

I am running Ubuntu 13.10 and fish 2.1.0. I want to write myself a Python script to do some tasks from the command line. The script will require command line arguments.

如何编写脚本,使fish可以请求并获取给定参数的可能值.潜在值列表是动态的.也就是说,它将以编程方式确定(例如,符合某些条件的文件夹名称列表).

How can I write my script such that fish can request and get possible values for a given argument. The list of potential values is dynamic. That is, it will be determined programatically (e.g. a list of folder names matching some criteria).

我想要的最终结果是这样的:

The end result I am aiming for is something like:

user@machine ~> myprog argument=fol<TAB>
folder1  folder2  folder3

其中myprog是我的脚本文件,argument是参数名称,而folder1等是由脚本中某些函数生成的有效值.

Where myprog is my script file, argument is the argument name and folder1 etc are valid values generated by some function in my script.

这可能吗?如果可以,怎么办?

Is this possible, and if so how?

推荐答案

改编自 zanchey在GitHub上的评论:

如果您的程序myprog具有--_completion选项,则可以为myprog编写一个看起来像这样的完成存根:

If you have a program myprog which takes the --_completion option, you can write a single completion stub for myprog that looks like this:

complete --command myprog --arguments '(myprog --_completion (commandline -cp)'

您的程序将以myprog --_completion myprog some arguments here的形式被调用,您可以使用适当的完成内容进行响应.它应该仅返回正在完成的当前令牌(您也可以使用(commandline -ct)将其传递给程序,或者您自己对其进行令牌化),然后可选地带有选项卡和简短描述.多个补全用换行分隔.

Your program will then get invoked as myprog --_completion myprog some arguments here, and you can respond with the appropriate completions. It should return only the current token that is being completed (you could also pass this to the program with (commandline -ct), or tokenise it yourself), followed optionally by a tab and a short description. Multiple completions are separated with new lines.

注意:

  • --_completion is a convention suggested by the python-selfcompletion library, but you can use anything you want, and this answer is not Python-specific
  • There is no way to specify the default completion as described in dbarnett/python-selfcompletion#2 (GitHub comment). You would definitely have to make a short stub for each command.

特别是对于Python脚本,以下库可能会在将来的某个时候支持鱼类补全(但目前尚不支持):

For Python scripts specifically, the following libraries may support fish completions at some point in the future (but they don't yet):

  • argcomplete
  • python-selfcompletion

这篇关于如何从我自己的脚本中提供制表符补全来捕获shell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆