Skip to content

Commit

Permalink
Also make the did-you-mean template pluggable
Browse files Browse the repository at this point in the history
  • Loading branch information
meatballhat committed May 17, 2022
1 parent 68bd490 commit 5bb9f45
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
9 changes: 5 additions & 4 deletions app.go
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions godoc-current.txt
Expand Up @@ -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}}
Expand Down
2 changes: 1 addition & 1 deletion suggestions.go
Expand Up @@ -47,5 +47,5 @@ func suggestCommand(commands []*Command, provided string) (suggestion string) {
}
}

return fmt.Sprintf(didYouMeanTemplate, suggestion)
return fmt.Sprintf(SuggestDidYouMeanTemplate, suggestion)
}
8 changes: 4 additions & 4 deletions suggestions_test.go
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
5 changes: 3 additions & 2 deletions testdata/godoc-v2.x.txt
Expand Up @@ -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}}
Expand Down

0 comments on commit 5bb9f45

Please sign in to comment.