Allow a default function to argument

If user add an annotation with the
`cobra_annotations_zsh_completion_default_function` key and a value to a
function it will be used as a default completion for that command.

Signed-off-by: Chmouel Boudjnah <chmouel@chmouel.com>
This commit is contained in:
Chmouel Boudjnah 2019-07-05 07:42:52 +02:00
parent f2b07da1e2
commit 3fea27d230
No known key found for this signature in database
GPG key ID: 8E00E7B02807E02D
3 changed files with 27 additions and 4 deletions

View file

@ -13,6 +13,7 @@ import (
) )
const ( const (
zshCompArgumentDefaultComp = "cobra_annotations_zsh_completion_default_function"
zshCompArgumentAnnotation = "cobra_annotations_zsh_completion_argument_annotation" zshCompArgumentAnnotation = "cobra_annotations_zsh_completion_argument_annotation"
zshCompArgumentFilenameComp = "cobra_annotations_zsh_completion_argument_file_completion" zshCompArgumentFilenameComp = "cobra_annotations_zsh_completion_argument_file_completion"
zshCompArgumentWordComp = "cobra_annotations_zsh_completion_argument_word_completion" zshCompArgumentWordComp = "cobra_annotations_zsh_completion_argument_word_completion"
@ -25,6 +26,7 @@ var (
"extractFlags": zshCompExtractFlag, "extractFlags": zshCompExtractFlag,
"genFlagEntryForZshArguments": zshCompGenFlagEntryForArguments, "genFlagEntryForZshArguments": zshCompGenFlagEntryForArguments,
"extractArgsCompletions": zshCompExtractArgumentCompletionHintsForRendering, "extractArgsCompletions": zshCompExtractArgumentCompletionHintsForRendering,
"extractDefaultCompletion": zshCompExtractCommandDefaultCompletion,
} }
zshCompletionText = ` zshCompletionText = `
{{/* should accept Command (that contains subcommands) as parameter */}} {{/* should accept Command (that contains subcommands) as parameter */}}
@ -64,7 +66,7 @@ function {{genZshFuncName .}} {
{{" _arguments"}}{{range extractFlags .}} \ {{" _arguments"}}{{range extractFlags .}} \
{{genFlagEntryForZshArguments . -}} {{genFlagEntryForZshArguments . -}}
{{end}}{{range extractArgsCompletions .}} \ {{end}}{{range extractArgsCompletions .}} \
{{.}}{{end}} {{.}}{{end}}{{extractDefaultCompletion .}}
} }
{{end}} {{end}}
@ -165,6 +167,14 @@ func (c *Command) MarkZshCompPositionalArgumentWords(argPosition int, words ...s
return c.zshCompSetArgsAnnotations(annotation) return c.zshCompSetArgsAnnotations(annotation)
} }
func zshCompExtractCommandDefaultCompletion(c *Command) string {
annotationString, ok := c.Annotations[zshCompArgumentDefaultComp]
if !ok {
return ""
}
return fmt.Sprintf(" \\ \n '1: :%s'", annotationString)
}
func zshCompExtractArgumentCompletionHintsForRendering(c *Command) ([]string, error) { func zshCompExtractArgumentCompletionHintsForRendering(c *Command) ([]string, error) {
var result []string var result []string
annotation, err := c.zshCompGetArgsAnnotations() annotation, err := c.zshCompGetArgsAnnotations()

View file

@ -30,10 +30,10 @@ The generated completion script should be put somewhere in your `$fpath` named
specified for 2nd) and the command has `ValidArgs` it will be used as specified for 2nd) and the command has `ValidArgs` it will be used as
completion options for 1st argument. completion options for 1st argument.
* Argument completions only offered for commands with no subcommands. * Argument completions only offered for commands with no subcommands.
* Arbitary completion for a command. Using
`cobra_annotations_zsh_completion_default_function` annotation you can
specify a function for the first argument to a command to complete to.
### What's not yet Supported ### What's not yet Supported
* Custom completion scripts are not supported yet (We should probably create zsh
specific one, doesn't make sense to re-use the bash one as the functions will
be different).
* Whatever other feature you're looking for and doesn't exist :) * Whatever other feature you're looking for and doesn't exist :)

View file

@ -202,6 +202,19 @@ func TestGenZshCompletion(t *testing.T) {
`'1: :\("word1" "word2"\)'`, `'1: :\("word1" "word2"\)'`,
}, },
}, },
{
name: "argument completion when we have a default completion action.",
root: func() *Command {
r := genTestCommand("root", true)
var annotations = make(map[string]string)
annotations[zshCompArgumentDefaultComp] = "DEFAULT_COMPLETION_FUNCTION"
r.Annotations = annotations
return r
}(),
expectedExpressions: []string{
`'1: :DEFAULT_COMPLETION_FUNCTION'`,
},
},
{ {
name: "argument completion when command has ValidArgs and no annotation for argument at argPosition 1", name: "argument completion when command has ValidArgs and no annotation for argument at argPosition 1",
root: func() *Command { root: func() *Command {