Cer*_*gan 9 bash bash-completion
我正在创建一个函数来为我使用的命令提供可编程完成功能(在http://www.debian-administration.org/articles/317的帮助下).shell脚本用法如下:
script.sh command [command options]
Run Code Online (Sandbox Code Playgroud)
其中command可以是'foo'或'bar','foo'的命令选项是'a_foo = value'和'b_foo = value','bar'的命令选项是'a_bar = value'和'b_bar = value' .
这是我正在使用的配置:
_script() {
local cur command all_commands
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
command="${COMP_WORDS[1]}"
all_commands="foo bar"
case "${command}" in
foo)
COMPREPLY=( $(compgen -W "--a_foo --b_foo" -- ${cur}) ); return 0;;
bar)
COMPREPLY=( $(compgen -W "--a_bar --b_bar" -- ${cur}) ); return 0;;
*) ;;
esac
COMPREPLY=( $(compgen -W "${all_commands}" -- ${cur}) )
return 0
}
complete -F _script script.sh
Run Code Online (Sandbox Code Playgroud)
这主要是按照我的意愿行事:
% script.sh f[TAB]
Run Code Online (Sandbox Code Playgroud)
完成:
% script.sh foo
Run Code Online (Sandbox Code Playgroud)
(根据需要设有尾随空格)
但是,这个:
% script.sh foo a[TAB]
Run Code Online (Sandbox Code Playgroud)
完成:
% script.sh foo a_foo
Run Code Online (Sandbox Code Playgroud)
(也有尾随空格)
我想用'='替换尾随空格.或者,我愿意将传递给compgen的值更改为"--a_foo = --b_foo =",在这种情况下,我可以删除尾随空格.
不幸的是,该命令不在我的控制之下,因此我无法将命令行选项更改为"--a_foo value"而不是"--a_foo = value".
Cou*_*gar 12
首先,您需要将=添加到COMPREPLY:
COMPREPLY=( $(compgen -W "--a_foo= --b_foo=" -- ${cur}) )
Run Code Online (Sandbox Code Playgroud)
接下来你需要告诉完成不要在=后添加空格
compopt -o nospace
Run Code Online (Sandbox Code Playgroud)
所以,你的脚本行应该是:
foo)
COMPREPLY=( $(compgen -W "--a_foo= --b_foo=" -- ${cur}) ); compopt -o nospace; return 0;;
bar)
COMPREPLY=( $(compgen -W "--a_bar= --b_bar=" -- ${cur}) ); compopt -o nospace; return 0;;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1612 次 |
| 最近记录: |