我已经创建了自己的bash完成功能。我做了几次修改。完成本身适合我,但我在短期选项-a
,-p
,-h
意思是,当我输入editcfg -fil<TAB>
时,它会自动填充到editcfg -file
(-file
之后的空格)。
但是,如果我输入editcfg -p<TAB>
,它就会在-p
之后赢得自动航空。
功能:
_editcfg ()
{
local cur prev opts presets u_opts;
COMPREPLY=();
cur="${COMP_WORDS[COMP_CWORD]}";
prev="${COMP_WORDS[COMP_CWORD-1]}";
opts=("-n" "-p" "-file" "-a" "-verbose" "-version" "-h");
presets=("default" "empty");
u_opts=();
for i in "${opts[@]}";
do
for j in "${COMP_WORDS[@]}";
do
if [[ "$i" == "$j" ]]; then
continue 2;
fi;
done;
u_opts+=("$i");
done;
case ${prev} in
-p)
COMPREPLY=($(compgen -W "${presets[*]}" -- ${cur}));
return 0
;;
-file)
COMPREPLY=($(compgen -fd -- ${cur} 2>/dev/null));
return 0
;;
-h | -version)
u_opts=();
return 0
;;
esac;
COMPREPLY=($(compgen -W "${u_opts[*]}" -- ${cur}));
return 0
}
我错过了什么?
由于
答案 0 :(得分:1)
我更新了第二个for
循环并且运行正常:
_editcfg ()
{
local cur prev opts presets u_opts;
COMPREPLY=();
cur="${COMP_WORDS[COMP_CWORD]}";
prev="${COMP_WORDS[COMP_CWORD-1]}";
opts=("-n" "-p" "-file" "-a" "-verbose" "-version" "-h");
presets=("default" "empty");
u_opts=();
for i in "${opts[@]}"; do
for ((j = 0; j < COMP_CWORD; ++j)) do
if [[ "$i" == "${COMP_WORDS[j]}" ]]; then
continue 2;
fi;
done;
u_opts+=("$i");
done;
case ${prev} in
-p)
COMPREPLY=($(compgen -W "${presets[*]}" -- ${cur}));
return 0
;;
-file)
COMPREPLY=($(compgen -fd -- ${cur} 2>/dev/null));
return 0
;;
-h | -version)
u_opts=();
return 0
;;
esac;
COMPREPLY=($(compgen -W "${u_opts[*]}" -- ${cur}));
return 0
}