1
0
Fork 0
mirror of https://github.com/spf13/cobra synced 2025-04-10 16:57:19 +00:00

Apply code review feedback

Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com>
Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
This commit is contained in:
ccoVeille 2025-02-08 21:08:11 +01:00
parent f80a6ab97a
commit 7b78b0536a
No known key found for this signature in database
2 changed files with 10 additions and 10 deletions

View file

@ -94,7 +94,7 @@ type Command struct {
// ArgAliases is List of aliases for ValidArgs. // ArgAliases is List of aliases for ValidArgs.
// These are not suggested to the user in the shell completion, // These are not suggested to the user in the shell completion,
// but accepted if entered manually. // but accepted if entered manually.
ArgAliases []CompletionChoice ArgAliases []string
// BashCompletionFunction is custom bash functions used by the legacy bash autocompletion generator. // BashCompletionFunction is custom bash functions used by the legacy bash autocompletion generator.
// For portability with other shells, it is recommended to instead use ValidArgsFunction // For portability with other shells, it is recommended to instead use ValidArgsFunction

View file

@ -120,20 +120,20 @@ type CompletionOptions struct {
// CompletionChoice is a string that can be used for completions // CompletionChoice is a string that can be used for completions
// //
// two formats are supported: // two formats are supported:
// - the name of the flag // - the completion choice
// - the name of the flag with its 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 description using the TAB separator. // [CompletionChoiceWithDescription] 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 CompletionChoice = string
// CompletionFunc is a function that provides completion results. // CompletionFunc is a function that provides completion results.
type CompletionFunc func(cmd *Command, args []CompletionChoice, toComplete string) ([]string, ShellCompDirective) type CompletionFunc func(cmd *Command, args []string, toComplete string) ([]CompletionChoice, ShellCompDirective)
// CompletionChoiceWithDescription returns a CompletionChoice with a description // CompletionChoiceWithDescription returns a [CompletionChoice] with a description by using the TAB delimited format.
func CompletionChoiceWithDescription(name, desc string) CompletionChoice { func CompletionChoiceWithDescription(choice string, description string) CompletionChoice {
return name + "\t" + desc return choice + "\t" + description
} }
// NoFileCompletions can be used to disable file completion for commands that should // NoFileCompletions can be used to disable file completion for commands that should
@ -141,7 +141,7 @@ func CompletionChoiceWithDescription(name, desc string) CompletionChoice {
// //
// 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 []CompletionChoice, toComplete string) ([]string, ShellCompDirective) { func NoFileCompletions(cmd *Command, args []string, toComplete string) ([]CompletionChoice, ShellCompDirective) {
return nil, ShellCompDirectiveNoFileComp return nil, ShellCompDirectiveNoFileComp
} }
@ -151,7 +151,7 @@ func NoFileCompletions(cmd *Command, args []CompletionChoice, toComplete string)
// 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 []CompletionChoice, directive ShellCompDirective) CompletionFunc {
return func(cmd *Command, args []CompletionChoice, toComplete string) ([]string, ShellCompDirective) { return func(cmd *Command, args []string, toComplete string) ([]CompletionChoice, ShellCompDirective) {
return choices, directive return choices, directive
} }
} }