English 中文(简体)
Accessing bash completions for specific commands programmatically
原标题:

I m trying to write a small command launcher application, and would like to use bash s tab completions in my own completion system. I ve been able to get a list of completions for general commands using compgen -abck.
However, I would also like to get completions for specific commands: for instance, the input git p should display completion for git s commands.

Is there any way I can use compgen to do this? If not, are there any other ways I can get a list of completions programmatically?

[EDIT: To clarify, I m not trying to provide completion to bash - my app is a GUI command launcher. I d simply like to use bash s existing completions in my own app.]

最佳回答

I don t really know how it works, but the awesome window manager uses the following Lua code for getting access to bash completion s result:

https://github.com/awesomeWM/awesome/blob/master/lib/awful/completion.lua#L119

  1. Via complete -p we find complete -o bashdefault -o default -o nospace -F _git git. We remember "_git" for later.
  2. The length of "git l" is 5, so we set COMP_COUNT=6. We are completing the first argument to "git", so COMP_CWORD=1.

All together we use the following script:

__print_completions() {
    printf  %s
  "${COMPREPLY[@]}"
}

# load bash-completion functions
source /etc/bash_completion

# load git s completion function
_completion_loader git

COMP_WORDS=(git l)
COMP_LINE= git l 
COMP_POINT=6
COMP_CWORD=1
_git
__print_completions

Output: "log"

问题回答

Check in the /etc/bash_completion.d/ directory. This is where the different command completion scripts stay.

Quite an old question, but in the mean time I ve implemented a script that handles this to reuse completions with ZSH

Here a simple but working example in bash :

function foo_completion()
{
    local currentWord=${COMP_WORDS[COMP_CWORD]}
    local completionList=""

    case "${COMP_CWORD}" in
        "1")
            completionList="command1 command2 command3";
            ;;
        "2")
            completionList="param1 param2 param3";
            ;;
    esac

    COMPREPLY=( $( compgen -W "${completionList}" -- ${currentWord} ) )
}

complete -F foo_completion foo

With this kind of code, you will get commandN completed when you type "foo c" + tab and paramN completed when you type "foo command1 p" + tab

You can compute the completion list from the command help.

my2c





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签