mirror of
https://github.com/spf13/cobra
synced 2025-05-06 13:27:26 +00:00
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:
parent
f2b07da1e2
commit
3fea27d230
3 changed files with 27 additions and 4 deletions
|
@ -13,6 +13,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
zshCompArgumentDefaultComp = "cobra_annotations_zsh_completion_default_function"
|
||||
zshCompArgumentAnnotation = "cobra_annotations_zsh_completion_argument_annotation"
|
||||
zshCompArgumentFilenameComp = "cobra_annotations_zsh_completion_argument_file_completion"
|
||||
zshCompArgumentWordComp = "cobra_annotations_zsh_completion_argument_word_completion"
|
||||
|
@ -25,6 +26,7 @@ var (
|
|||
"extractFlags": zshCompExtractFlag,
|
||||
"genFlagEntryForZshArguments": zshCompGenFlagEntryForArguments,
|
||||
"extractArgsCompletions": zshCompExtractArgumentCompletionHintsForRendering,
|
||||
"extractDefaultCompletion": zshCompExtractCommandDefaultCompletion,
|
||||
}
|
||||
zshCompletionText = `
|
||||
{{/* should accept Command (that contains subcommands) as parameter */}}
|
||||
|
@ -64,7 +66,7 @@ function {{genZshFuncName .}} {
|
|||
{{" _arguments"}}{{range extractFlags .}} \
|
||||
{{genFlagEntryForZshArguments . -}}
|
||||
{{end}}{{range extractArgsCompletions .}} \
|
||||
{{.}}{{end}}
|
||||
{{.}}{{end}}{{extractDefaultCompletion .}}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
|
@ -165,6 +167,14 @@ func (c *Command) MarkZshCompPositionalArgumentWords(argPosition int, words ...s
|
|||
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) {
|
||||
var result []string
|
||||
annotation, err := c.zshCompGetArgsAnnotations()
|
||||
|
|
|
@ -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
|
||||
completion options for 1st argument.
|
||||
* 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
|
||||
|
||||
* 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 :)
|
||||
|
|
|
@ -202,6 +202,19 @@ func TestGenZshCompletion(t *testing.T) {
|
|||
`'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",
|
||||
root: func() *Command {
|
||||
|
|
Loading…
Add table
Reference in a new issue