mirror of
https://github.com/spf13/cobra
synced 2025-04-07 15:29:16 +00:00
Replace more 'string' with 'Completion'
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
This commit is contained in:
parent
865cca0289
commit
19e833b707
5 changed files with 34 additions and 28 deletions
|
@ -35,7 +35,7 @@ const (
|
||||||
// This function can be called multiple times before and/or after completions are added to
|
// This function can be called multiple times before and/or after completions are added to
|
||||||
// the array. Each time this function is called with the same array, the new
|
// the array. Each time this function is called with the same array, the new
|
||||||
// ActiveHelp line will be shown below the previous ones when completion is triggered.
|
// ActiveHelp line will be shown below the previous ones when completion is triggered.
|
||||||
func AppendActiveHelp(compArray []string, activeHelpStr string) []string {
|
func AppendActiveHelp(compArray []Completion, activeHelpStr string) []Completion {
|
||||||
return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr))
|
return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1272,8 +1272,8 @@ func (c *Command) InitDefaultHelpCmd() {
|
||||||
Short: "Help about any command",
|
Short: "Help about any command",
|
||||||
Long: `Help provides help for any command in the application.
|
Long: `Help provides help for any command in the application.
|
||||||
Simply type ` + c.DisplayName() + ` help [path to command] for full details.`,
|
Simply type ` + c.DisplayName() + ` help [path to command] for full details.`,
|
||||||
ValidArgsFunction: func(c *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
|
ValidArgsFunction: func(c *Command, args []string, toComplete string) ([]Completion, ShellCompDirective) {
|
||||||
var completions []string
|
var completions []Completion
|
||||||
cmd, _, e := c.Root().Find(args)
|
cmd, _, e := c.Root().Find(args)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return nil, ShellCompDirectiveNoFileComp
|
return nil, ShellCompDirectiveNoFileComp
|
||||||
|
@ -1285,7 +1285,7 @@ Simply type ` + c.DisplayName() + ` help [path to command] for full details.`,
|
||||||
for _, subCmd := range cmd.Commands() {
|
for _, subCmd := range cmd.Commands() {
|
||||||
if subCmd.IsAvailableCommand() || subCmd == cmd.helpCommand {
|
if subCmd.IsAvailableCommand() || subCmd == cmd.helpCommand {
|
||||||
if strings.HasPrefix(subCmd.Name(), toComplete) {
|
if strings.HasPrefix(subCmd.Name(), toComplete) {
|
||||||
completions = append(completions, fmt.Sprintf("%s\t%s", subCmd.Name(), subCmd.Short))
|
completions = append(completions, CompletionWithDesc(subCmd.Name(), subCmd.Short))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -502,7 +502,7 @@ func (c *Command) getCompletions(args []string) (*Command, []Completion, ShellCo
|
||||||
for _, subCmd := range finalCmd.Commands() {
|
for _, subCmd := range finalCmd.Commands() {
|
||||||
if subCmd.IsAvailableCommand() || subCmd == finalCmd.helpCommand {
|
if subCmd.IsAvailableCommand() || subCmd == finalCmd.helpCommand {
|
||||||
if strings.HasPrefix(subCmd.Name(), toComplete) {
|
if strings.HasPrefix(subCmd.Name(), toComplete) {
|
||||||
completions = append(completions, fmt.Sprintf("%s\t%s", subCmd.Name(), subCmd.Short))
|
completions = append(completions, CompletionWithDesc(subCmd.Name(), subCmd.Short))
|
||||||
}
|
}
|
||||||
directive = ShellCompDirectiveNoFileComp
|
directive = ShellCompDirectiveNoFileComp
|
||||||
}
|
}
|
||||||
|
@ -587,7 +587,7 @@ func getFlagNameCompletions(flag *pflag.Flag, toComplete string) []Completion {
|
||||||
flagName := "--" + flag.Name
|
flagName := "--" + flag.Name
|
||||||
if strings.HasPrefix(flagName, toComplete) {
|
if strings.HasPrefix(flagName, toComplete) {
|
||||||
// Flag without the =
|
// Flag without the =
|
||||||
completions = append(completions, fmt.Sprintf("%s\t%s", flagName, flag.Usage))
|
completions = append(completions, CompletionWithDesc(flagName, flag.Usage))
|
||||||
|
|
||||||
// Why suggest both long forms: --flag and --flag= ?
|
// Why suggest both long forms: --flag and --flag= ?
|
||||||
// This forces the user to *always* have to type either an = or a space after the flag name.
|
// This forces the user to *always* have to type either an = or a space after the flag name.
|
||||||
|
@ -599,13 +599,13 @@ func getFlagNameCompletions(flag *pflag.Flag, toComplete string) []Completion {
|
||||||
// if len(flag.NoOptDefVal) == 0 {
|
// if len(flag.NoOptDefVal) == 0 {
|
||||||
// // Flag requires a value, so it can be suffixed with =
|
// // Flag requires a value, so it can be suffixed with =
|
||||||
// flagName += "="
|
// flagName += "="
|
||||||
// completions = append(completions, fmt.Sprintf("%s\t%s", flagName, flag.Usage))
|
// completions = append(completions, CompletionWithDesc(flagName, flag.Usage))
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
flagName = "-" + flag.Shorthand
|
flagName = "-" + flag.Shorthand
|
||||||
if len(flag.Shorthand) > 0 && strings.HasPrefix(flagName, toComplete) {
|
if len(flag.Shorthand) > 0 && strings.HasPrefix(flagName, toComplete) {
|
||||||
completions = append(completions, fmt.Sprintf("%s\t%s", flagName, flag.Usage))
|
completions = append(completions, CompletionWithDesc(flagName, flag.Usage))
|
||||||
}
|
}
|
||||||
|
|
||||||
return completions
|
return completions
|
||||||
|
|
|
@ -41,8 +41,8 @@ cmd := &cobra.Command{
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return addRepo(args)
|
return addRepo(args)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||||
var comps []string
|
var comps []cobra.Completion
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
comps = cobra.AppendActiveHelp(comps, "You must choose a name for the repo you are adding")
|
comps = cobra.AppendActiveHelp(comps, "You must choose a name for the repo you are adding")
|
||||||
} else if len(args) == 1 {
|
} else if len(args) == 1 {
|
||||||
|
@ -75,7 +75,7 @@ This command does not take any more arguments
|
||||||
Providing Active Help for flags is done in the same fashion as for nouns, but using the completion function registered for the flag. For example:
|
Providing Active Help for flags is done in the same fashion as for nouns, but using the completion function registered for the flag. For example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
_ = cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
_ = cmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||||
if len(args) != 2 {
|
if len(args) != 2 {
|
||||||
return cobra.AppendActiveHelp(nil, "You must first specify the chart to install before the --version flag can be completed"), cobra.ShellCompDirectiveNoFileComp
|
return cobra.AppendActiveHelp(nil, "You must first specify the chart to install before the --version flag can be completed"), cobra.ShellCompDirectiveNoFileComp
|
||||||
}
|
}
|
||||||
|
@ -112,10 +112,10 @@ should or should not be added (instead of reading the environment variable direc
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||||
activeHelpLevel := cobra.GetActiveHelpConfig(cmd)
|
activeHelpLevel := cobra.GetActiveHelpConfig(cmd)
|
||||||
|
|
||||||
var comps []string
|
var comps []cobra.Completion
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
if activeHelpLevel != "off" {
|
if activeHelpLevel != "off" {
|
||||||
comps = cobra.AppendActiveHelp(comps, "You must choose a name for the repo you are adding")
|
comps = cobra.AppendActiveHelp(comps, "You must choose a name for the repo you are adding")
|
||||||
|
|
|
@ -177,7 +177,7 @@ cmd := &cobra.Command{
|
||||||
RunE: func(cmd *cobra.Command, args []string) {
|
RunE: func(cmd *cobra.Command, args []string) {
|
||||||
RunGet(args[0])
|
RunGet(args[0])
|
||||||
},
|
},
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||||
if len(args) != 0 {
|
if len(args) != 0 {
|
||||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
}
|
}
|
||||||
|
@ -211,7 +211,7 @@ ShellCompDirectiveNoFileComp
|
||||||
|
|
||||||
// Indicates that the returned completions should be used as file extension filters.
|
// Indicates that the returned completions should be used as file extension filters.
|
||||||
// For example, to complete only files of the form *.json or *.yaml:
|
// For example, to complete only files of the form *.json or *.yaml:
|
||||||
// return []string{"yaml", "json"}, ShellCompDirectiveFilterFileExt
|
// return []cobra.Completion{"yaml", "json"}, cobra.ShellCompDirectiveFilterFileExt
|
||||||
// For flags, using MarkFlagFilename() and MarkPersistentFlagFilename()
|
// For flags, using MarkFlagFilename() and MarkPersistentFlagFilename()
|
||||||
// is a shortcut to using this directive explicitly.
|
// is a shortcut to using this directive explicitly.
|
||||||
//
|
//
|
||||||
|
@ -219,13 +219,13 @@ ShellCompDirectiveFilterFileExt
|
||||||
|
|
||||||
// Indicates that only directory names should be provided in file completion.
|
// Indicates that only directory names should be provided in file completion.
|
||||||
// For example:
|
// For example:
|
||||||
// return nil, ShellCompDirectiveFilterDirs
|
// return nil, cobra.ShellCompDirectiveFilterDirs
|
||||||
// For flags, using MarkFlagDirname() is a shortcut to using this directive explicitly.
|
// For flags, using MarkFlagDirname() is a shortcut to using this directive explicitly.
|
||||||
//
|
//
|
||||||
// To request directory names within another directory, the returned completions
|
// To request directory names within another directory, the returned completions
|
||||||
// should specify a single directory name within which to search. For example,
|
// should specify a single directory name within which to search. For example,
|
||||||
// to complete directories within "themes/":
|
// to complete directories within "themes/":
|
||||||
// return []string{"themes"}, ShellCompDirectiveFilterDirs
|
// return []cobra.Completion{"themes"}, cobra.ShellCompDirectiveFilterDirs
|
||||||
//
|
//
|
||||||
ShellCompDirectiveFilterDirs
|
ShellCompDirectiveFilterDirs
|
||||||
|
|
||||||
|
@ -293,8 +293,8 @@ As for nouns, Cobra provides a way of defining dynamic completion of flags. To
|
||||||
|
|
||||||
```go
|
```go
|
||||||
flagName := "output"
|
flagName := "output"
|
||||||
cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||||
return []string{"json", "table", "yaml"}, cobra.ShellCompDirectiveDefault
|
return []cobra.Completion{"json", "table", "yaml"}, cobra.ShellCompDirectiveDefault
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
Notice that calling `RegisterFlagCompletionFunc()` is done through the `command` with which the flag is associated. In our example this dynamic completion will give results like so:
|
Notice that calling `RegisterFlagCompletionFunc()` is done through the `command` with which the flag is associated. In our example this dynamic completion will give results like so:
|
||||||
|
@ -327,8 +327,8 @@ cmd.MarkFlagFilename(flagName, "yaml", "json")
|
||||||
or
|
or
|
||||||
```go
|
```go
|
||||||
flagName := "output"
|
flagName := "output"
|
||||||
cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||||
return []string{"yaml", "json"}, ShellCompDirectiveFilterFileExt})
|
return []cobra.Completion{"yaml", "json"}, cobra.ShellCompDirectiveFilterFileExt})
|
||||||
```
|
```
|
||||||
|
|
||||||
### Limit flag completions to directory names
|
### Limit flag completions to directory names
|
||||||
|
@ -341,15 +341,15 @@ cmd.MarkFlagDirname(flagName)
|
||||||
or
|
or
|
||||||
```go
|
```go
|
||||||
flagName := "output"
|
flagName := "output"
|
||||||
cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||||
return nil, cobra.ShellCompDirectiveFilterDirs
|
return nil, cobra.ShellCompDirectiveFilterDirs
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
To limit completions of flag values to directory names *within another directory* you can use a combination of `RegisterFlagCompletionFunc()` and `ShellCompDirectiveFilterDirs` like so:
|
To limit completions of flag values to directory names *within another directory* you can use a combination of `RegisterFlagCompletionFunc()` and `ShellCompDirectiveFilterDirs` like so:
|
||||||
```go
|
```go
|
||||||
flagName := "output"
|
flagName := "output"
|
||||||
cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||||
return []string{"themes"}, cobra.ShellCompDirectiveFilterDirs
|
return []cobra.Completion{"themes"}, cobra.ShellCompDirectiveFilterDirs
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
### Descriptions for completions
|
### Descriptions for completions
|
||||||
|
@ -370,15 +370,21 @@ $ helm s[tab]
|
||||||
search (search for a keyword in charts) show (show information of a chart) status (displays the status of the named release)
|
search (search for a keyword in charts) show (show information of a chart) status (displays the status of the named release)
|
||||||
```
|
```
|
||||||
|
|
||||||
Cobra allows you to add descriptions to your own completions. Simply add the description text after each completion, following a `\t` separator. This technique applies to completions returned by `ValidArgs`, `ValidArgsFunction` and `RegisterFlagCompletionFunc()`. For example:
|
Cobra allows you to add descriptions to your own completions. Simply add the description text after each completion, following a `\t` separator. Cobra provides the helper function `CompletionWithDesc(string, string)` to create a completion with a description. This technique applies to completions returned by `ValidArgs`, `ValidArgsFunction` and `RegisterFlagCompletionFunc()`. For example:
|
||||||
```go
|
```go
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
||||||
return []string{"harbor\tAn image registry", "thanos\tLong-term metrics"}, cobra.ShellCompDirectiveNoFileComp
|
return []cobra.Completion{
|
||||||
|
cobra.CompletionWithDesc("harbor", "An image registry"),
|
||||||
|
cobra.CompletionWithDesc("thanos", "Long-term metrics")
|
||||||
|
}, cobra.ShellCompDirectiveNoFileComp
|
||||||
}}
|
}}
|
||||||
```
|
```
|
||||||
or
|
or
|
||||||
```go
|
```go
|
||||||
ValidArgs: []string{"bash\tCompletions for bash", "zsh\tCompletions for zsh"}
|
ValidArgs: []cobra.Completion{
|
||||||
|
cobra.CompletionWithDesc("bash", "Completions for bash"),
|
||||||
|
cobra.CompletionWithDesc("zsh", "Completions for zsh")
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
If you don't want to show descriptions in the completions, you can add `--no-descriptions` to the default `completion` command to disable them, like:
|
If you don't want to show descriptions in the completions, you can add `--no-descriptions` to the default `completion` command to disable them, like:
|
||||||
|
|
Loading…
Reference in a new issue