2

I'm in a situation where I need to set up bash completion for a series of applications whose binaries all start with the same prefix -- let's call it ... blargh. I do not and can not know which blargh* applications a person using this completion program will have on their machine (although in my situation, the prefix is unique enough that it won't interfere with normal operation of other commands).

That is, I'd like to do something along the lines of:

complete -F _function_name "blargh*" "./blargh*"

Right now, what I've got is a pretty run-of-the-mill completion program:

# blargh-completions.bash

_blargh_completions () {
    COMPREPLY=...
}

_blargh_init_completions () {
    local blargh
    for blargh in blarghABC blarghXYZ blargh123 blarghXXX-debug blarghARGH ; do
        complete -F _blargh_completions "$blargh"
        complete -F _blargh_completions "./$blargh"
    done
}

_blargh_init_completions

Where I've got a decent initial guess at the application names, but I still have to instruct users to double-check the script and keep their local copy's for loop updated as necessary, which isn't ideal.

Additionally, I'm a little unclear on where I'd install a completion script that handles multiple programs. All of the options have shortcomings:

  • I could put it in compatdir (e.g. /etc/bash_completion.d/): This lets the script work with multiple commands, but at the same time all docs suggest this location is deprecated and should not be used.
  • I could put it in completionsdir (e.g. /usr/share/bash-completion/completions/): Except the script has to have the same name as the command in this directory so I don't know how to make it work in this case.
  • I could append it to the user's ~/.bash_completion file, except then automated updates / uninstallations become difficult.

So my question is: How (if it's even possible) can I write and install a completion script that applies to a wildcarded name pattern like blargh* without knowing the precise command names ahead of time?

1 Answer 1

2
+50

Unfortunately the complete command does not support command name wildcards/prefixes.

However, you may be able to introduce a wrapper function named blargh which takes a suffix (a subcommand name I presume) then create a bash completion function for it:


blargh() {
    command "blargh$@"
}

_blargh_completion() {
    ((COMP_CWORD >= 1)) || return
    local subcommand="${COMP_WORDS[1]}"
    if ((COMP_CWORD == 1)); then
        # ${subcommand} is a prefix
        # emit subcommand names that start with it
    else
        # complete arguments for the ${subcommand}
    fi
}

complete -F _blargh_completion blargh

This way, blarghcreate ... can also be invoked as blargh create ... and the latter way of invocation can be autocompleted. It then can be put into /usr/share/bash-completion/completions/blargh because technically it is a completion for the blargh command. :-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .