fix default suggestions template for multiple suggestions

This commit is contained in:
John Brunton 2020-06-25 13:35:13 +01:00
parent eb5c65715d
commit 410e43b3fe
2 changed files with 24 additions and 5 deletions

View file

@ -546,8 +546,7 @@ func (c *Command) SuggestionsTemplate() string {
return `
Did you mean this?
{{range .}}{{print "\t" .}}{{end}}
`
{{range .}}{{print "\t" . "\n"}}{{end}}`
}
func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {

View file

@ -1205,9 +1205,8 @@ 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,
Use: "times",
Run: emptyRun,
}
rootCmd.AddCommand(timesCmd)
rootCmd.SetSuggestionsTemplate(`
@ -1221,6 +1220,27 @@ customized suggestions: {{range .}}{{.}}{{end}}`)
}
}
func TestDefaultSuggestionsTemplateMultiLine(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
timeCmd := &Command{
Use: "time",
Run: emptyRun,
}
rootCmd.AddCommand(timeCmd)
timesCmd := &Command{
Use: "times",
Run: emptyRun,
}
rootCmd.AddCommand(timesCmd)
output, _ := executeCommand(rootCmd, "tim")
expected := "Error: unknown command \"tim\" for \"root\"\n\nDid you mean this?\n\ttime\n\ttimes\n\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}