mirror of
https://github.com/spf13/cobra
synced 2025-04-07 07:19:16 +00:00
Rename helpers after feedback made during the review
Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com> Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
This commit is contained in:
parent
ae509db00b
commit
8bcda5b137
3 changed files with 26 additions and 26 deletions
|
@ -82,7 +82,7 @@ type Command struct {
|
||||||
Example string
|
Example string
|
||||||
|
|
||||||
// ValidArgs is list of all valid non-flag arguments that are accepted in shell completions
|
// ValidArgs is list of all valid non-flag arguments that are accepted in shell completions
|
||||||
ValidArgs []CompletionChoice
|
ValidArgs []Completion
|
||||||
// ValidArgsFunction is an optional function that provides valid non-flag arguments for shell completion.
|
// ValidArgsFunction is an optional function that provides valid non-flag arguments for shell completion.
|
||||||
// It is a dynamic version of using ValidArgs.
|
// It is a dynamic version of using ValidArgs.
|
||||||
// Only one of ValidArgs and ValidArgsFunction can be used for a command.
|
// Only one of ValidArgs and ValidArgsFunction can be used for a command.
|
||||||
|
|
|
@ -117,22 +117,22 @@ type CompletionOptions struct {
|
||||||
HiddenDefaultCmd bool
|
HiddenDefaultCmd bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompletionChoice is a string that can be used for completions
|
// Completion is a string that can be used for completions
|
||||||
//
|
//
|
||||||
// two formats are supported:
|
// two formats are supported:
|
||||||
// - the completion choice
|
// - the completion choice
|
||||||
// - the completion choice with a textual description (separated by a TAB).
|
// - the completion choice with a textual description (separated by a TAB).
|
||||||
//
|
//
|
||||||
// [CompletionChoiceWithDescription] can be used to create a completion string with a textual description.
|
// [CompletionWithDesc] can be used to create a completion string with a textual description.
|
||||||
//
|
//
|
||||||
// Note: Go type alias is used to provide a more descriptive name in the documentation, but any string can be used.
|
// Note: Go type alias is used to provide a more descriptive name in the documentation, but any string can be used.
|
||||||
type CompletionChoice = string
|
type Completion = string
|
||||||
|
|
||||||
// CompletionFunc is a function that provides completion results.
|
// CompletionFunc is a function that provides completion results.
|
||||||
type CompletionFunc func(cmd *Command, args []string, toComplete string) ([]CompletionChoice, ShellCompDirective)
|
type CompletionFunc func(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective)
|
||||||
|
|
||||||
// CompletionChoiceWithDescription returns a [CompletionChoice] with a description by using the TAB delimited format.
|
// CompletionWithDesc returns a [Completion] with a description by using the TAB delimited format.
|
||||||
func CompletionChoiceWithDescription(choice string, description string) CompletionChoice {
|
func CompletionWithDesc(choice string, description string) Completion {
|
||||||
return choice + "\t" + description
|
return choice + "\t" + description
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,7 +141,7 @@ func CompletionChoiceWithDescription(choice string, description string) Completi
|
||||||
//
|
//
|
||||||
// This method satisfies [CompletionFunc].
|
// This method satisfies [CompletionFunc].
|
||||||
// It can be used with [Command.RegisterFlagCompletionFunc] and for [Command.ValidArgsFunction].
|
// It can be used with [Command.RegisterFlagCompletionFunc] and for [Command.ValidArgsFunction].
|
||||||
func NoFileCompletions(cmd *Command, args []string, toComplete string) ([]CompletionChoice, ShellCompDirective) {
|
func NoFileCompletions(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective) {
|
||||||
return nil, ShellCompDirectiveNoFileComp
|
return nil, ShellCompDirectiveNoFileComp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,8 +150,8 @@ func NoFileCompletions(cmd *Command, args []string, toComplete string) ([]Comple
|
||||||
//
|
//
|
||||||
// This method returns a function that satisfies [CompletionFunc]
|
// This method returns a function that satisfies [CompletionFunc]
|
||||||
// It can be used with [Command.RegisterFlagCompletionFunc] and for [Command.ValidArgsFunction].
|
// It can be used with [Command.RegisterFlagCompletionFunc] and for [Command.ValidArgsFunction].
|
||||||
func FixedCompletions(choices []CompletionChoice, directive ShellCompDirective) CompletionFunc {
|
func FixedCompletions(choices []Completion, directive ShellCompDirective) CompletionFunc {
|
||||||
return func(cmd *Command, args []string, toComplete string) ([]CompletionChoice, ShellCompDirective) {
|
return func(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective) {
|
||||||
return choices, directive
|
return choices, directive
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -306,7 +306,7 @@ type SliceValue interface {
|
||||||
GetSlice() []string
|
GetSlice() []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) getCompletions(args []string) (*Command, []CompletionChoice, ShellCompDirective, error) {
|
func (c *Command) getCompletions(args []string) (*Command, []Completion, ShellCompDirective, error) {
|
||||||
// The last argument, which is not completely typed by the user,
|
// The last argument, which is not completely typed by the user,
|
||||||
// should not be part of the list of arguments
|
// should not be part of the list of arguments
|
||||||
toComplete := args[len(args)-1]
|
toComplete := args[len(args)-1]
|
||||||
|
@ -334,7 +334,7 @@ func (c *Command) getCompletions(args []string) (*Command, []CompletionChoice, S
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Unable to find the real command. E.g., <program> someInvalidCmd <TAB>
|
// Unable to find the real command. E.g., <program> someInvalidCmd <TAB>
|
||||||
return c, []CompletionChoice{}, ShellCompDirectiveDefault, fmt.Errorf("unable to find a command for arguments: %v", trimmedArgs)
|
return c, []Completion{}, ShellCompDirectiveDefault, fmt.Errorf("unable to find a command for arguments: %v", trimmedArgs)
|
||||||
}
|
}
|
||||||
finalCmd.ctx = c.ctx
|
finalCmd.ctx = c.ctx
|
||||||
|
|
||||||
|
@ -364,7 +364,7 @@ func (c *Command) getCompletions(args []string) (*Command, []CompletionChoice, S
|
||||||
|
|
||||||
// Parse the flags early so we can check if required flags are set
|
// Parse the flags early so we can check if required flags are set
|
||||||
if err = finalCmd.ParseFlags(finalArgs); err != nil {
|
if err = finalCmd.ParseFlags(finalArgs); err != nil {
|
||||||
return finalCmd, []CompletionChoice{}, ShellCompDirectiveDefault, fmt.Errorf("Error while parsing flags from args %v: %s", finalArgs, err.Error())
|
return finalCmd, []Completion{}, ShellCompDirectiveDefault, fmt.Errorf("Error while parsing flags from args %v: %s", finalArgs, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
realArgCount := finalCmd.Flags().NArg()
|
realArgCount := finalCmd.Flags().NArg()
|
||||||
|
@ -376,14 +376,14 @@ func (c *Command) getCompletions(args []string) (*Command, []CompletionChoice, S
|
||||||
if flagErr != nil {
|
if flagErr != nil {
|
||||||
// If error type is flagCompError and we don't want flagCompletion we should ignore the error
|
// If error type is flagCompError and we don't want flagCompletion we should ignore the error
|
||||||
if _, ok := flagErr.(*flagCompError); !(ok && !flagCompletion) {
|
if _, ok := flagErr.(*flagCompError); !(ok && !flagCompletion) {
|
||||||
return finalCmd, []CompletionChoice{}, ShellCompDirectiveDefault, flagErr
|
return finalCmd, []Completion{}, ShellCompDirectiveDefault, flagErr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look for the --help or --version flags. If they are present,
|
// Look for the --help or --version flags. If they are present,
|
||||||
// there should be no further completions.
|
// there should be no further completions.
|
||||||
if helpOrVersionFlagPresent(finalCmd) {
|
if helpOrVersionFlagPresent(finalCmd) {
|
||||||
return finalCmd, []CompletionChoice{}, ShellCompDirectiveNoFileComp, nil
|
return finalCmd, []Completion{}, ShellCompDirectiveNoFileComp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// We only remove the flags from the arguments if DisableFlagParsing is not set.
|
// We only remove the flags from the arguments if DisableFlagParsing is not set.
|
||||||
|
@ -412,11 +412,11 @@ func (c *Command) getCompletions(args []string) (*Command, []CompletionChoice, S
|
||||||
return finalCmd, subDir, ShellCompDirectiveFilterDirs, nil
|
return finalCmd, subDir, ShellCompDirectiveFilterDirs, nil
|
||||||
}
|
}
|
||||||
// Directory completion
|
// Directory completion
|
||||||
return finalCmd, []CompletionChoice{}, ShellCompDirectiveFilterDirs, nil
|
return finalCmd, []Completion{}, ShellCompDirectiveFilterDirs, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var completions []CompletionChoice
|
var completions []Completion
|
||||||
var directive ShellCompDirective
|
var directive ShellCompDirective
|
||||||
|
|
||||||
// Enforce flag groups before doing flag completions
|
// Enforce flag groups before doing flag completions
|
||||||
|
@ -558,7 +558,7 @@ func (c *Command) getCompletions(args []string) (*Command, []CompletionChoice, S
|
||||||
if completionFn != nil {
|
if completionFn != nil {
|
||||||
// Go custom completion defined for this flag or command.
|
// Go custom completion defined for this flag or command.
|
||||||
// Call the registered completion function to get the completions.
|
// Call the registered completion function to get the completions.
|
||||||
var comps []CompletionChoice
|
var comps []Completion
|
||||||
comps, directive = completionFn(finalCmd, finalArgs, toComplete)
|
comps, directive = completionFn(finalCmd, finalArgs, toComplete)
|
||||||
completions = append(completions, comps...)
|
completions = append(completions, comps...)
|
||||||
}
|
}
|
||||||
|
@ -578,12 +578,12 @@ func helpOrVersionFlagPresent(cmd *Command) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFlagNameCompletions(flag *pflag.Flag, toComplete string) []CompletionChoice {
|
func getFlagNameCompletions(flag *pflag.Flag, toComplete string) []Completion {
|
||||||
if nonCompletableFlag(flag) {
|
if nonCompletableFlag(flag) {
|
||||||
return []CompletionChoice{}
|
return []Completion{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var completions []CompletionChoice
|
var completions []Completion
|
||||||
flagName := "--" + flag.Name
|
flagName := "--" + flag.Name
|
||||||
if strings.HasPrefix(flagName, toComplete) {
|
if strings.HasPrefix(flagName, toComplete) {
|
||||||
// Flag without the =
|
// Flag without the =
|
||||||
|
@ -611,8 +611,8 @@ func getFlagNameCompletions(flag *pflag.Flag, toComplete string) []CompletionCho
|
||||||
return completions
|
return completions
|
||||||
}
|
}
|
||||||
|
|
||||||
func completeRequireFlags(finalCmd *Command, toComplete string) []CompletionChoice {
|
func completeRequireFlags(finalCmd *Command, toComplete string) []Completion {
|
||||||
var completions []CompletionChoice
|
var completions []Completion
|
||||||
|
|
||||||
doCompleteRequiredFlags := func(flag *pflag.Flag) {
|
doCompleteRequiredFlags := func(flag *pflag.Flag) {
|
||||||
if _, present := flag.Annotations[BashCompOneRequiredFlag]; present {
|
if _, present := flag.Annotations[BashCompOneRequiredFlag]; present {
|
||||||
|
|
|
@ -2872,10 +2872,10 @@ func TestFixedCompletions(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFixedCompletionsWithCompletionChoiceHelpers(t *testing.T) {
|
func TestFixedCompletionsWithCompletionHelpers(t *testing.T) {
|
||||||
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
|
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
|
||||||
// here we are mixing string, CompletionChoice and CompletionChoiceWithDescription
|
// here we are mixing string, [Completion] and [CompletionWithDesc]
|
||||||
choices := []string{"apple", CompletionChoice("banana"), CompletionChoiceWithDescription("orange", "orange are orange")}
|
choices := []string{"apple", Completion("banana"), CompletionWithDesc("orange", "orange are orange")}
|
||||||
childCmd := &Command{
|
childCmd := &Command{
|
||||||
Use: "child",
|
Use: "child",
|
||||||
ValidArgsFunction: FixedCompletions(choices, ShellCompDirectiveNoFileComp),
|
ValidArgsFunction: FixedCompletions(choices, ShellCompDirectiveNoFileComp),
|
||||||
|
|
Loading…
Reference in a new issue