Add --init-completion flag to print completion code
This flag takes as input 'bash' or 'zsh' and generates the completion code for the specified shell.
This commit is contained in:
38
help.go
38
help.go
@@ -286,3 +286,41 @@ func checkCommandCompletions(c *Context, name string) bool {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func checkInitCompletion(c *Context) (bool, error) {
|
||||
if c.IsSet(InitCompletionFlag.Name) {
|
||||
shell := c.String(InitCompletionFlag.Name)
|
||||
progName := os.Args[0]
|
||||
switch shell {
|
||||
case "bash":
|
||||
fmt.Print(bashCompletionCode(progName))
|
||||
return true, nil
|
||||
case "zsh":
|
||||
fmt.Print(zshCompletionCode(progName))
|
||||
return true, nil
|
||||
default:
|
||||
return false, fmt.Errorf("--init-completion value cannot be '%s'", shell)
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func bashCompletionCode(progName string) string {
|
||||
var template = `_cli_bash_autocomplete() {
|
||||
local cur opts base;
|
||||
COMPREPLY=();
|
||||
cur="${COMP_WORDS[COMP_CWORD]}";
|
||||
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion );
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) );
|
||||
return 0;
|
||||
};
|
||||
complete -F _cli_bash_autocomplete %s`
|
||||
return fmt.Sprintf(template, progName)
|
||||
}
|
||||
|
||||
func zshCompletionCode(progName string) string {
|
||||
var template = `autoload -U compinit && compinit;
|
||||
autoload -U bashcompinit && bashcompinit;`
|
||||
|
||||
return template + "\n" + bashCompletionCode(progName)
|
||||
}
|
||||
|
Reference in New Issue
Block a user