Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Level Bash Completion

Tags:

I currently have a Bash completion file which completes a single parameter from a list of allowed commands for a script (called "pbt"). This is the working Bash Completion file:

_pbt_complete() {   local cur goals    COMPREPLY=()   cur=${COMP_WORDS[COMP_CWORD]}   goals='asadmin clean deploy'   cur=`echo $cur`   COMPREPLY=($(compgen -W "${goals}" ${cur})) }  complete -F _pbt_complete pbt 

So if i call

pbt <tab> 

Bash completes to all allowed commands (asadmin, clean, deploy), which is okay.

Now i want to add a "second" level to the completion. So for example if i type

pbt asadmin <tab> 

it should complete all options that are only available inside the asadmin-"environment" (which i'll also define inside the bash completion file), say for example pbt asadmin [start-domain|stop-domain] But if i type

pbt deploy <tab> 

it should complete to another set of options, for example pbt deploy [all|current]. So the options for the second command should always depend on the first command. How can i do that in the completion file?

like image 353
Wolkenarchitekt Avatar asked Mar 14 '11 18:03

Wolkenarchitekt


People also ask

How do you autocomplete in bash?

Bash completion is a bash function that allows you to auto complete commands or arguments by typing partially commands or arguments, then pressing the [Tab] key. This will help you when writing the bash command in terminal.

What is bash completion?

What is Bash completion? Bash completion is a functionality through which Bash helps users type their commands more quickly and easily. It does this by presenting possible options when users press the Tab key while typing a command.

What is kubectl bash completion?

The 'kubectl completion bash' command enables the auto-completion of the kubectl script. Sourcing the completion script, you need to install bash completion first. Here is the output of the above command: However, before you install the bash completion, check whether your server already has it or not.

How do you auto complete in Linux?

Command auto-completion is available when using the CLI shell. You can set up CLI command auto-completion in your operating system using the kollacli command command. This command generates a script you can save to the operating system. Copy and paste the output to a file, for example a file named /etc/bash_completion.


1 Answers

Thanks to mkb's comment i looked into the p4-example, which was - unlike the Git example ;) - simple enough for me to adapt it to my case. Here is the working version which does exactly what i asked for:

have pbt && _pbt_complete() {   local cur prev    COMPREPLY=()   cur=${COMP_WORDS[COMP_CWORD]}   prev=${COMP_WORDS[COMP_CWORD-1]}    if [ $COMP_CWORD -eq 1 ]; then     COMPREPLY=( $(compgen -W "asadmin deploy" -- $cur) )   elif [ $COMP_CWORD -eq 2 ]; then     case "$prev" in       "asadmin")         COMPREPLY=( $(compgen -W "start-domain stop-domain" -- $cur) )         ;;       "deploy")         COMPREPLY=( $(compgen -W "all current" -- $cur) )         ;;       *)         ;;     esac   fi    return 0 } && complete -F _pbt_complete pbt 
like image 181
Wolkenarchitekt Avatar answered Oct 04 '22 03:10

Wolkenarchitekt