mirror of
https://github.com/spf13/cobra
synced 2025-05-07 13:57:21 +00:00
add suggestions template
This commit is contained in:
parent
04318720db
commit
eb5c65715d
2 changed files with 46 additions and 4 deletions
31
command.go
31
command.go
|
@ -201,6 +201,8 @@ type Command struct {
|
|||
helpCommand *Command
|
||||
// versionTemplate is the version template defined by user.
|
||||
versionTemplate string
|
||||
// suggestionsTemplate is the suggestions template defined by user.
|
||||
suggestionsTemplate string
|
||||
|
||||
// inReader is a reader defined by the user that replaces stdin
|
||||
inReader io.Reader
|
||||
|
@ -284,6 +286,11 @@ func (c *Command) SetVersionTemplate(s string) {
|
|||
c.versionTemplate = s
|
||||
}
|
||||
|
||||
// SetSuggestionsTemplate sets suggestions template. Can be defined by Application.
|
||||
func (c *Command) SetSuggestionsTemplate(s string) {
|
||||
c.suggestionsTemplate = s
|
||||
}
|
||||
|
||||
// SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands.
|
||||
// The user should not have a cyclic dependency on commands.
|
||||
func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
|
||||
|
@ -527,6 +534,22 @@ func (c *Command) VersionTemplate() string {
|
|||
`
|
||||
}
|
||||
|
||||
// SuggestionsTemplate return suggestions template for the command.
|
||||
func (c *Command) SuggestionsTemplate() string {
|
||||
if c.suggestionsTemplate != "" {
|
||||
return c.suggestionsTemplate
|
||||
}
|
||||
|
||||
if c.HasParent() {
|
||||
return c.parent.SuggestionsTemplate()
|
||||
}
|
||||
return `
|
||||
|
||||
Did you mean this?
|
||||
{{range .}}{{print "\t" .}}{{end}}
|
||||
`
|
||||
}
|
||||
|
||||
func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {
|
||||
flag := fs.Lookup(name)
|
||||
if flag == nil {
|
||||
|
@ -639,10 +662,10 @@ func (c *Command) findSuggestions(arg string) string {
|
|||
}
|
||||
suggestionsString := ""
|
||||
if suggestions := c.SuggestionsFor(arg); len(suggestions) > 0 {
|
||||
suggestionsString += "\n\nDid you mean this?\n"
|
||||
for _, s := range suggestions {
|
||||
suggestionsString += fmt.Sprintf("\t%v\n", s)
|
||||
}
|
||||
var suggestionsBuffer bytes.Buffer
|
||||
suggestions := c.SuggestionsFor(arg)
|
||||
tmpl(&suggestionsBuffer, c.SuggestionsTemplate(), suggestions)
|
||||
suggestionsString += suggestionsBuffer.String()
|
||||
}
|
||||
return suggestionsString
|
||||
}
|
||||
|
|
|
@ -1202,6 +1202,25 @@ func TestSuggestions(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSuggestionsTemplate(t *testing.T) {
|
||||
rootCmd := &Command{Use: "root", Run: emptyRun}
|
||||
timesCmd := &Command{
|
||||
Use: "times",
|
||||
SuggestFor: []string{"counts"},
|
||||
Run: emptyRun,
|
||||
}
|
||||
rootCmd.AddCommand(timesCmd)
|
||||
rootCmd.SetSuggestionsTemplate(`
|
||||
customized suggestions: {{range .}}{{.}}{{end}}`)
|
||||
|
||||
output, _ := executeCommand(rootCmd, "time")
|
||||
expected := "Error: unknown command \"time\" for \"root\"\ncustomized suggestions: times\nRun 'root --help' for usage.\n"
|
||||
|
||||
if output != expected {
|
||||
t.Errorf("Unexpected response.\nExpected:\n %q\nGot:\n %q\n", expected, output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveCommand(t *testing.T) {
|
||||
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
|
||||
childCmd := &Command{Use: "child", Run: emptyRun}
|
||||
|
|
Loading…
Add table
Reference in a new issue