Also make the did-you-mean template pluggable

This commit is contained in:
Dan Buch 2022-05-17 09:24:17 -04:00
parent 68bd4903fd
commit 5bb9f453dc
Signed by: meatballhat
GPG Key ID: A12F782281063434
5 changed files with 16 additions and 13 deletions

9
app.go
View File

@ -11,7 +11,7 @@ import (
"time"
)
const didYouMeanTemplate = "Did you mean '%s'?"
const suggestDidYouMeanTemplate = "Did you mean %q?"
var (
changeLogURL = "https://github.com/urfave/cli/blob/main/docs/CHANGELOG.md"
@ -21,8 +21,9 @@ var (
fmt.Sprintf("Must be `func(*Context`)` or `func(*Context) error). %s", contactSysadmin)+
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
SuggestFlag SuggestFlagFunc = suggestFlag
SuggestCommand SuggestCommandFunc = suggestCommand
SuggestFlag SuggestFlagFunc = suggestFlag
SuggestCommand SuggestCommandFunc = suggestCommand
SuggestDidYouMeanTemplate string = suggestDidYouMeanTemplate
)
// App is the main structure of a cli application. It is recommended that
@ -361,7 +362,7 @@ func (a *App) suggestFlagFromError(err error, command string) (string, error) {
return "", err
}
return fmt.Sprintf(didYouMeanTemplate+"\n\n", suggestion), nil
return fmt.Sprintf(SuggestDidYouMeanTemplate+"\n\n", suggestion), nil
}
// RunAndExitOnError calls .Run() and exits non-zero if an error was returned

View File

@ -27,8 +27,9 @@ application:
VARIABLES
var (
SuggestFlag SuggestFlagFunc = suggestFlag
SuggestCommand SuggestCommandFunc = suggestCommand
SuggestFlag SuggestFlagFunc = suggestFlag
SuggestCommand SuggestCommandFunc = suggestCommand
SuggestDidYouMeanTemplate string = suggestDidYouMeanTemplate
)
var AppHelpTemplate = `NAME:
{{.Name}}{{if .Usage}} - {{.Usage}}{{end}}

View File

@ -47,5 +47,5 @@ func suggestCommand(commands []*Command, provided string) (suggestion string) {
}
}
return fmt.Sprintf(didYouMeanTemplate, suggestion)
return fmt.Sprintf(SuggestDidYouMeanTemplate, suggestion)
}

View File

@ -56,7 +56,7 @@ func TestSuggestFlagFromError(t *testing.T) {
)
// Then
expect(t, res, fmt.Sprintf(didYouMeanTemplate+"\n\n", testCase.expected))
expect(t, res, fmt.Sprintf(SuggestDidYouMeanTemplate+"\n\n", testCase.expected))
}
}
@ -116,7 +116,7 @@ func TestSuggestCommand(t *testing.T) {
res := suggestCommand(app.Commands, testCase.provided)
// Then
expect(t, res, fmt.Sprintf(didYouMeanTemplate, testCase.expected))
expect(t, res, fmt.Sprintf(SuggestDidYouMeanTemplate, testCase.expected))
}
}
@ -140,7 +140,7 @@ func ExampleApp_Suggest() {
// Output:
// Incorrect Usage. flag provided but not defined: -nema
//
// Did you mean '--name'?
// Did you mean "--name"?
//
// (this space intentionally left blank)
}
@ -181,7 +181,7 @@ func ExampleApp_Suggest_command() {
// Output:
// Incorrect Usage: flag provided but not defined: -sliming
//
// Did you mean '--smiling'?
// Did you mean "--smiling"?
//
// (this space intentionally left blank)
}

View File

@ -27,8 +27,9 @@ application:
VARIABLES
var (
SuggestFlag SuggestFlagFunc = suggestFlag
SuggestCommand SuggestCommandFunc = suggestCommand
SuggestFlag SuggestFlagFunc = suggestFlag
SuggestCommand SuggestCommandFunc = suggestCommand
SuggestDidYouMeanTemplate string = suggestDidYouMeanTemplate
)
var AppHelpTemplate = `NAME:
{{.Name}}{{if .Usage}} - {{.Usage}}{{end}}