shell怎么使用制表符,如何从我自己的脚本中提供制表符补全来捕获shell?

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.

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

folder1 folder2 folder3

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?

解决方案

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)'

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.

Notes:

--_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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值