【发布时间】:2021-01-07 23:28:02
【问题描述】:
我已经尝试(但惨遭失败)编写一个可以任意深度自动完成的 bash 完成脚本。尽管阅读了多个 SO 帖子和一些博客和文档,但我没有超过两个 :-\。例如,我可以到达:
$ seuss tweedle beetle
但无法访问seuss tweedle beetle puddle
我什至不需要每个完成后的函数,只需要选项本身。我尝试从以下帖子中修改脚本,但我所做的一切都会中断。
Nested bash autocompletion script
How do I autocomplete nested, multi-level subcommands?
这是我尝试过的:
_seuss()
{
local one two three four
one=${COMP_WORDS[COMP_CWORD]}
two=${COMP_WORDS[COMP_CWORD-1]}
three=${COMP_WORDS[COMP_CWORD-2]}
four=${COMP_WORDS[COMP_CWORD-3]}
case ${COMP_CWORD} in
1)
COMPREPLY=($(compgen -W "fox tweedle" -- ${one}))
;;
2)
case ${two} in
fox)
COMPREPLY=($(compgen -W "socks" -- ${one}))
case ${three} in
socks)
COMPREPLY=($(compgen -W "box clocks" -- ${one}))
;;
box)
COMPREPLY=($(compgen -W "knox" -- ${one}))
;;
esac
;;
tweedle)
COMPREPLY=($(compgen -W "beetle poodle" -- ${one}))
case ${three} in
beetle)
COMPREPLY=($(compgen -W "puddle battle" -- ${one}))
;;
poddle)
COMPREPLY=($(compgen -W "noodle" -- ${one}))
;;
esac
;;
esac
;;
*)
COMPREPLY=()
;;
esac
}
complete -F _seuss seuss
但这只会导致:
$ seuss fox sox
我似乎无法获得box clocks。
【问题讨论】:
-
我对@987654328@ 一无所知,所以您能否详细说明一下预期的行为是什么?
-
苏斯只是一个不存在的任意例子。预期的行为是:
seuss <tab> fox <tab> sox <tab> box|clocks -
自动填充候选人是由职位决定的吗?还是通过前面的论点?还是通过之前的所有论点?
标签: bash autocomplete