I am trying to develop an autocomplete or tab-complete feature for my own set of commands.
For example, assume foo is my binary:
CLI>> foo [TAB] [TAB]
It should show the main commands configure
and show
.
Then if I select configure
, it should show the subcommands CM
, DSP
and NPU
:
CLI>> foo [TAB] [TAB]
DSP NPU CM`
I only know how to tab-complete and display for the first level - how can I get the second level as well?
I will place this in /etc/completion.d
.
My Code:
_foo()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
words=("${COMP_WORDS[@]}")
cword=$COMP_CWORD
opts="configure show"
}
I'm stuck as how to add sub commands CM
DSP
NPU
under configure
.
Here's an example script for your two-level case (with a section for subcommands of
show
to show how it's done - you can just delete those three lines if they're not relevant to your case):Hopefully it's fairly obvious from that example how you'd extend it to three-level commands etc., as well.