commit
e66ce91db1
@ -1,10 +1,10 @@
|
||||
*.coverprofile
|
||||
*.orig
|
||||
node_modules/
|
||||
vendor
|
||||
.idea
|
||||
internal/*/built-example
|
||||
coverage.txt
|
||||
/.local/
|
||||
/site/
|
||||
|
||||
*.exe
|
||||
|
@ -0,0 +1 @@
|
||||
cli.urfave.org
|
@ -0,0 +1 @@
|
||||
../CODE_OF_CONDUCT.md
|
@ -0,0 +1,21 @@
|
||||
# Welcome to urfave/cli
|
||||
|
||||
[![GoDoc](https://godoc.org/github.com/urfave/cli?status.svg)](https://pkg.go.dev/github.com/urfave/cli/v2)
|
||||
[![codebeat](https://codebeat.co/badges/0a8f30aa-f975-404b-b878-5fab3ae1cc5f)](https://codebeat.co/projects/github-com-urfave-cli)
|
||||
[![Go Report Card](https://goreportcard.com/badge/urfave/cli)](https://goreportcard.com/report/urfave/cli)
|
||||
[![codecov](https://codecov.io/gh/urfave/cli/branch/main/graph/badge.svg)](https://codecov.io/gh/urfave/cli)
|
||||
|
||||
`urfave/cli` is a simple, fast, and fun package for building command line apps in Go. The
|
||||
goal is to enable developers to write fast and distributable command line applications in
|
||||
an expressive way.
|
||||
|
||||
These are the guides for each major supported version:
|
||||
|
||||
- [`v2`](./v2/)
|
||||
- [`v1`](./v1/)
|
||||
|
||||
In addition to the version-specific guides, these other documents are available:
|
||||
|
||||
- [CONTRIBUTING](./CONTRIBUTING/)
|
||||
- [CODE OF CONDUCT](./CODE_OF_CONDUCT/)
|
||||
- [RELEASING](./RELEASING/)
|
@ -0,0 +1 @@
|
||||
manual.md
|
@ -0,0 +1 @@
|
||||
manual.md
|
@ -0,0 +1,5 @@
|
||||
mkdocs-git-revision-date-localized-plugin~=1.0
|
||||
mkdocs-material-extensions~=1.0
|
||||
mkdocs-material~=8.2
|
||||
mkdocs~=1.3
|
||||
pygments~=2.12
|
@ -0,0 +1,62 @@
|
||||
# NOTE: the mkdocs dependencies will need to be installed out of
|
||||
# band until this whole thing gets more automated:
|
||||
#
|
||||
# pip install -r mkdocs-requirements.txt
|
||||
#
|
||||
|
||||
site_name: urfave/cli
|
||||
site_url: https://cli.urfave.org/
|
||||
repo_url: https://github.com/urfave/cli
|
||||
edit_uri: edit/main/docs/
|
||||
nav:
|
||||
- Home: index.md
|
||||
- v2 Manual: v2/index.md
|
||||
- v1 Manual: v1/index.md
|
||||
theme:
|
||||
name: material
|
||||
palette:
|
||||
- media: "(prefers-color-scheme: light)"
|
||||
scheme: default
|
||||
toggle:
|
||||
icon: material/brightness-4
|
||||
name: dark mode
|
||||
- media: "(prefers-color-scheme: dark)"
|
||||
scheme: slate
|
||||
toggle:
|
||||
icon: material/brightness-7
|
||||
name: light mode
|
||||
plugins:
|
||||
- git-revision-date-localized
|
||||
- search
|
||||
# NOTE: this is the recommended configuration from
|
||||
# https://squidfunk.github.io/mkdocs-material/setup/extensions/#recommended-configuration
|
||||
markdown_extensions:
|
||||
- abbr
|
||||
- admonition
|
||||
- attr_list
|
||||
- def_list
|
||||
- footnotes
|
||||
- meta
|
||||
- md_in_html
|
||||
- toc:
|
||||
permalink: true
|
||||
- pymdownx.arithmatex:
|
||||
generic: true
|
||||
- pymdownx.betterem:
|
||||
smart_enable: all
|
||||
- pymdownx.caret
|
||||
- pymdownx.details
|
||||
- pymdownx.emoji:
|
||||
emoji_index: !!python/name:materialx.emoji.twemoji
|
||||
emoji_generator: !!python/name:materialx.emoji.to_svg
|
||||
- pymdownx.highlight
|
||||
- pymdownx.inlinehilite
|
||||
- pymdownx.keys
|
||||
- pymdownx.mark
|
||||
- pymdownx.smartsymbols
|
||||
- pymdownx.superfences
|
||||
- pymdownx.tabbed:
|
||||
alternate_style: true
|
||||
- pymdownx.tasklist:
|
||||
custom_checkbox: true
|
||||
- pymdownx.tilde
|
@ -0,0 +1,75 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/antzucaro/matchr"
|
||||
)
|
||||
|
||||
const didYouMeanTemplate = "Did you mean '%s'?"
|
||||
|
||||
func (a *App) suggestFlagFromError(err error, command string) (string, error) {
|
||||
flag, parseErr := flagFromError(err)
|
||||
if parseErr != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
flags := a.Flags
|
||||
if command != "" {
|
||||
cmd := a.Command(command)
|
||||
if cmd == nil {
|
||||
return "", err
|
||||
}
|
||||
flags = cmd.Flags
|
||||
}
|
||||
|
||||
suggestion := a.suggestFlag(flags, flag)
|
||||
if len(suggestion) == 0 {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fmt.Sprintf(didYouMeanTemplate+"\n\n", suggestion), nil
|
||||
}
|
||||
|
||||
func (a *App) suggestFlag(flags []Flag, provided string) (suggestion string) {
|
||||
distance := 0.0
|
||||
|
||||
for _, flag := range flags {
|
||||
flagNames := flag.Names()
|
||||
if !a.HideHelp {
|
||||
flagNames = append(flagNames, HelpFlag.Names()...)
|
||||
}
|
||||
for _, name := range flagNames {
|
||||
newDistance := matchr.JaroWinkler(name, provided, true)
|
||||
if newDistance > distance {
|
||||
distance = newDistance
|
||||
suggestion = name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(suggestion) == 1 {
|
||||
suggestion = "-" + suggestion
|
||||
} else if len(suggestion) > 1 {
|
||||
suggestion = "--" + suggestion
|
||||
}
|
||||
|
||||
return suggestion
|
||||
}
|
||||
|
||||
// suggestCommand takes a list of commands and a provided string to suggest a
|
||||
// command name
|
||||
func suggestCommand(commands []*Command, provided string) (suggestion string) {
|
||||
distance := 0.0
|
||||
for _, command := range commands {
|
||||
for _, name := range append(command.Names(), helpName, helpAlias) {
|
||||
newDistance := matchr.JaroWinkler(name, provided, true)
|
||||
if newDistance > distance {
|
||||
distance = newDistance
|
||||
suggestion = name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf(didYouMeanTemplate, suggestion)
|
||||
}
|
@ -0,0 +1,188 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSuggestFlag(t *testing.T) {
|
||||
// Given
|
||||
app := testApp()
|
||||
|
||||
for _, testCase := range []struct {
|
||||
provided, expected string
|
||||
}{
|
||||
{"", ""},
|
||||
{"a", "--another-flag"},
|
||||
{"hlp", "--help"},
|
||||
{"k", ""},
|
||||
{"s", "-s"},
|
||||
} {
|
||||
// When
|
||||
res := app.suggestFlag(app.Flags, testCase.provided)
|
||||
|
||||
// Then
|
||||
expect(t, res, testCase.expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSuggestFlagHideHelp(t *testing.T) {
|
||||
// Given
|
||||
app := testApp()
|
||||
app.HideHelp = true
|
||||
|
||||
// When
|
||||
res := app.suggestFlag(app.Flags, "hlp")
|
||||
|
||||
// Then
|
||||
expect(t, res, "--fl")
|
||||
}
|
||||
|
||||
func TestSuggestFlagFromError(t *testing.T) {
|
||||
// Given
|
||||
app := testApp()
|
||||
|
||||
for _, testCase := range []struct {
|
||||
command, provided, expected string
|
||||
}{
|
||||
{"", "hel", "--help"},
|
||||
{"", "soccer", "--socket"},
|
||||
{"config", "anot", "--another-flag"},
|
||||
} {
|
||||
// When
|
||||
res, _ := app.suggestFlagFromError(
|
||||
errors.New(providedButNotDefinedErrMsg+testCase.provided),
|
||||
testCase.command,
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(t, res, fmt.Sprintf(didYouMeanTemplate+"\n\n", testCase.expected))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSuggestFlagFromErrorWrongError(t *testing.T) {
|
||||
// Given
|
||||
app := testApp()
|
||||
|
||||
// When
|
||||
_, err := app.suggestFlagFromError(errors.New("invalid"), "")
|
||||
|
||||
// Then
|
||||
expect(t, true, err != nil)
|
||||
}
|
||||
|
||||
func TestSuggestFlagFromErrorWrongCommand(t *testing.T) {
|
||||
// Given
|
||||
app := testApp()
|
||||
|
||||
// When
|
||||
_, err := app.suggestFlagFromError(
|
||||
errors.New(providedButNotDefinedErrMsg+"flag"),
|
||||
"invalid",
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(t, true, err != nil)
|
||||
}
|
||||
|
||||
func TestSuggestFlagFromErrorNoSuggestion(t *testing.T) {
|
||||
// Given
|
||||
app := testApp()
|
||||
|
||||
// When
|
||||
_, err := app.suggestFlagFromError(
|
||||
errors.New(providedButNotDefinedErrMsg+""),
|
||||
"",
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(t, true, err != nil)
|
||||
}
|
||||
|
||||
func TestSuggestCommand(t *testing.T) {
|
||||
// Given
|
||||
app := testApp()
|
||||
|
||||
for _, testCase := range []struct {
|
||||
provided, expected string
|
||||
}{
|
||||
{"", ""},
|
||||
{"conf", "config"},
|
||||
{"i", "i"},
|
||||
{"information", "info"},
|
||||
{"not-existing", "info"},
|
||||
} {
|
||||
// When
|
||||
res := suggestCommand(app.Commands, testCase.provided)
|
||||
|
||||
// Then
|
||||
expect(t, res, fmt.Sprintf(didYouMeanTemplate, testCase.expected))
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleApp_Suggest() {
|
||||
app := &App{
|
||||
Name: "greet",
|
||||
Suggest: true,
|
||||
HideHelp: true,
|
||||
HideHelpCommand: true,
|
||||
CustomAppHelpTemplate: "(this space intentionally left blank)\n",
|
||||
Flags: []Flag{
|
||||
&StringFlag{Name: "name", Value: "squirrel", Usage: "a name to say"},
|
||||
},
|
||||
Action: func(cCtx *Context) error {
|
||||
fmt.Printf("Hello %v\n", cCtx.String("name"))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
app.Run([]string{"greet", "--nema", "chipmunk"})
|
||||
// Output:
|
||||
// Incorrect Usage. flag provided but not defined: -nema
|
||||
//
|
||||
// Did you mean '--name'?
|
||||
//
|
||||
// (this space intentionally left blank)
|
||||
}
|
||||
|
||||
func ExampleApp_Suggest_command() {
|
||||
app := &App{
|
||||
Name: "greet",
|
||||
Suggest: true,
|
||||
HideHelp: true,
|
||||
HideHelpCommand: true,
|
||||
CustomAppHelpTemplate: "(this space intentionally left blank)\n",
|
||||
Flags: []Flag{
|
||||
&StringFlag{Name: "name", Value: "squirrel", Usage: "a name to say"},
|
||||
},
|
||||
Action: func(cCtx *Context) error {
|
||||
fmt.Printf("Hello %v\n", cCtx.String("name"))
|
||||
return nil
|
||||
},
|
||||
Commands: []*Command{
|
||||
{
|
||||
Name: "neighbors",
|
||||
CustomHelpTemplate: "(this space intentionally left blank)\n",
|
||||
Flags: []Flag{
|
||||
&BoolFlag{Name: "smiling"},
|
||||
},
|
||||
Action: func(cCtx *Context) error {
|
||||
if cCtx.Bool("smiling") {
|
||||
fmt.Println("๐")
|
||||
}
|
||||
fmt.Println("Hello, neighbors")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
app.Run([]string{"greet", "neighbors", "--sliming"})
|
||||
// Output:
|
||||
// Incorrect Usage: flag provided but not defined: -sliming
|
||||
//
|
||||
// Did you mean '--smiling'?
|
||||
//
|
||||
// (this space intentionally left blank)
|
||||
}
|