From 32d535b8130e4441d5ad3e40a168d2d6a7056c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Thu, 21 Dec 2023 00:54:53 +0100 Subject: [PATCH] Add build tag urfave_cli_no_suggest 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 --- app.go | 7 +++++-- godoc-current.txt | 4 ++-- help.go | 2 +- suggestions.go | 8 ++++++++ suggestions_test.go | 3 +++ testdata/godoc-v2.x.txt | 4 ++-- 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app.go b/app.go index f69a1939b1..1915d4fbeb 100644 --- a/app.go +++ b/app.go @@ -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 ) @@ -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 diff --git a/godoc-current.txt b/godoc-current.txt index 9be5107a98..46a260caf0 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -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: diff --git a/help.go b/help.go index 84bd77bc17..640e290452 100644 --- a/help.go +++ b/help.go @@ -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 } diff --git a/suggestions.go b/suggestions.go index c73a0012da..9d2b7a81e7 100644 --- a/suggestions.go +++ b/suggestions.go @@ -1,3 +1,6 @@ +//go:build !urfave_cli_no_suggest +// +build !urfave_cli_no_suggest + package cli import ( @@ -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 ( diff --git a/suggestions_test.go b/suggestions_test.go index 2136acdc46..8ccc3176d7 100644 --- a/suggestions_test.go +++ b/suggestions_test.go @@ -1,3 +1,6 @@ +//go:build !urfave_cli_no_suggest +// +build !urfave_cli_no_suggest + package cli import ( diff --git a/testdata/godoc-v2.x.txt b/testdata/godoc-v2.x.txt index 9be5107a98..46a260caf0 100644 --- a/testdata/godoc-v2.x.txt +++ b/testdata/godoc-v2.x.txt @@ -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: