Bash completion from another completion

JustAnotherCurious

I have a script that requires as an argument the name of command and arguments of that command.

So I want to write a completion function that would complete the name of the command and would complete the arguments for that command.

So I can complete the name of the command like this

if [[ "$COMP_CWORD" == 1 ]]; then
    COMPREPLY=( $( compgen -c ${COMP_WORDS[COMP_CWORD]} ))
else
    #Don't know what to write here
fi

So this will complete the first argument to list of shell commands that are available to run. And on second and other arguments I need a completion for ${COMP_WORDS[COMP_CWORD]} command.

I thought about removing first element from COMP_WORDS, decreasing COMP_CWORD by one and call a function _${COMP_WORDS[0]} with name that prefixes "_" to the command, because in many examples the function that completes a command has such name, but when I executed complete -p in bash I found that many commands are completed with functions that has different names. And as for me, such solution looks really bad.

I'm not a bash scripting guru so I just don't know where to start searching the solution.

anishsane

Your requirement is similar to the command completion for exec or time or xargs. These commands also take a command & that command's arguments as completion options.

Checking the bash_completion option for exec:

$ complete -p exec
complete -F _command exec

You can re-use the same function _command as your completion function..

Usage:

complete -F _command your-script.sh

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Blacklist program from bash completion

From Dev

Hide some commands from bash completion?

From Dev

prevent bash completion from altering underscore variable $_

From Dev

Where do bash completion functions come from?

From Dev

Bash completion comes back from the dead

From Dev

source command error from /usr/share/bash-completion/bash-completion when I open a terminal

From Dev

bash tab completion with spaces

From Dev

Disable bash tab completion

From Dev

Custom bash tab completion

From Dev

bash_completion for initscripts

From Dev

colorize bash_completion

From Dev

refresh bash tab completion

From Dev

Bash completion for own function

From Dev

No bash completion with gcutil

From Dev

Utilizing Bash Completion in a script

From Dev

Bash completion for cd alias

From Dev

Bash filename completion explanation

From Dev

Bash completion for `unrar`

From Dev

Bash completion with changing number?

From Dev

Documentation on bash completion

From Dev

Bash auto-completion

From Dev

Bash completion for nohup

From Java

How do I defer shell completion to another command in bash and zsh?

From Dev

Is it possible to call a block completion handler from another function in iOS?

From Dev

Swift: Notifying a ViewController from another using completion handler

From Dev

Can I use a helper function from a ZSH completion file in another

From Dev

How to remove a record from bash tab completion options

From Dev

Bash auto_completion with Xubuntu and xrdp from windows

From Dev

bash-completion (from Homebrew) not completing variables on OSX