Skip to content

Commit

Permalink
Add build tag urfave_cli_no_suggest
Browse files Browse the repository at this point in the history
Like urfave_cli_no_docs (in docs.go), allow to disable suggestions at
build time (and drop dependency on module github.com/xrash/smetrics)
with a new 'urfave_cli_no_suggest' build tag:

  go build -tags urfave_cli_no_suggest,urfave_cli_no_docs
  • Loading branch information
dolmen committed Dec 29, 2023
1 parent 1a4c1da commit a66c186
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 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 @@ -366,6 +366,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

0 comments on commit a66c186

Please sign in to comment.