Skip to content

Commit

Permalink
Merge pull request #1847 from dolmen/build-flag-disable-suggest
Browse files Browse the repository at this point in the history
v2: Add build tag urfave_cli_no_suggest
  • Loading branch information
dearchap authored Dec 30, 2023
2 parents 83ffe99 + 32d535b commit 7656c5f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
7 changes: 5 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ var (
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
ignoreFlagPrefix = "test." // this is to ignore test flags when adding flags from other packages

SuggestFlag SuggestFlagFunc = suggestFlag
SuggestCommand SuggestCommandFunc = suggestCommand
SuggestFlag SuggestFlagFunc = nil // initialized in suggestions.go unless built with urfave_cli_no_suggest
SuggestCommand SuggestCommandFunc = nil // initialized in suggestions.go unless built with urfave_cli_no_suggest
SuggestDidYouMeanTemplate string = suggestDidYouMeanTemplate
)

Expand Down Expand Up @@ -368,6 +368,9 @@ func (a *App) suggestFlagFromError(err error, command string) (string, error) {
hideHelp = hideHelp || cmd.HideHelp
}

if SuggestFlag == nil {
return "", err
}
suggestion := SuggestFlag(flags, flag, hideHelp)
if len(suggestion) == 0 {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ application:
VARIABLES

var (
SuggestFlag SuggestFlagFunc = suggestFlag
SuggestCommand SuggestCommandFunc = suggestCommand
SuggestFlag SuggestFlagFunc = nil // initialized in suggestions.go unless built with urfave_cli_no_suggest
SuggestCommand SuggestCommandFunc = nil // initialized in suggestions.go unless built with urfave_cli_no_suggest
SuggestDidYouMeanTemplate string = suggestDidYouMeanTemplate
)
var AppHelpTemplate = `NAME:
Expand Down
2 changes: 1 addition & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func ShowCommandHelp(ctx *Context, command string) error {

if ctx.App.CommandNotFound == nil {
errMsg := fmt.Sprintf("No help topic for '%v'", command)
if ctx.App.Suggest {
if ctx.App.Suggest && SuggestCommand != nil {
if suggestion := SuggestCommand(ctx.Command.Subcommands, command); suggestion != "" {
errMsg += ". " + suggestion
}
Expand Down
8 changes: 8 additions & 0 deletions suggestions.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !urfave_cli_no_suggest
// +build !urfave_cli_no_suggest

package cli

import (
Expand All @@ -6,6 +9,11 @@ import (
"github.com/xrash/smetrics"
)

func init() {
SuggestFlag = suggestFlag
SuggestCommand = suggestCommand
}

func jaroWinkler(a, b string) float64 {
// magic values are from https://github.com/xrash/smetrics/blob/039620a656736e6ad994090895784a7af15e0b80/jaro-winkler.go#L8
const (
Expand Down
3 changes: 3 additions & 0 deletions suggestions_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !urfave_cli_no_suggest
// +build !urfave_cli_no_suggest

package cli

import (
Expand Down
4 changes: 2 additions & 2 deletions testdata/godoc-v2.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ application:
VARIABLES

var (
SuggestFlag SuggestFlagFunc = suggestFlag
SuggestCommand SuggestCommandFunc = suggestCommand
SuggestFlag SuggestFlagFunc = nil // initialized in suggestions.go unless built with urfave_cli_no_suggest
SuggestCommand SuggestCommandFunc = nil // initialized in suggestions.go unless built with urfave_cli_no_suggest
SuggestDidYouMeanTemplate string = suggestDidYouMeanTemplate
)
var AppHelpTemplate = `NAME:
Expand Down

0 comments on commit 7656c5f

Please sign in to comment.