Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: urfave/cli
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.4.2
Choose a base ref
...
head repository: urfave/cli
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.4.3
Choose a head ref
  • 4 commits
  • 2 files changed
  • 2 contributors

Commits on Sep 17, 2020

  1. Verified

    This commit was signed with the committer’s verified signature.
    crazy-max CrazyMax
    Copy the full SHA
    a8e44a8 View commit details

Commits on Apr 21, 2022

  1. Copy the full SHA
    7a231c5 View commit details

Commits on Apr 22, 2022

  1. Copy the full SHA
    c4c15e1 View commit details
  2. Merge pull request #1360 from urfave/schollz-master

    show only subcommand flags with bash completion (#1186)
    meatballhat authored Apr 22, 2022
    Copy the full SHA
    372ee0a View commit details
Showing with 93 additions and 3 deletions.
  1. +10 −3 help.go
  2. +83 −0 help_test.go
13 changes: 10 additions & 3 deletions help.go
Original file line number Diff line number Diff line change
@@ -163,19 +163,26 @@ func DefaultCompleteWithFlags(cmd *Command) func(c *Context) {
return func(c *Context) {
if len(os.Args) > 2 {
lastArg := os.Args[len(os.Args)-2]

if strings.HasPrefix(lastArg, "-") {
printFlagSuggestions(lastArg, c.App.Flags, c.App.Writer)
if cmd != nil {
printFlagSuggestions(lastArg, cmd.Flags, c.App.Writer)

return
}

printFlagSuggestions(lastArg, c.App.Flags, c.App.Writer)

return
}
}

if cmd != nil {
printCommandSuggestions(cmd.Subcommands, c.App.Writer)
} else {
printCommandSuggestions(c.App.Commands, c.App.Writer)
return
}

printCommandSuggestions(c.App.Commands, c.App.Writer)
}
}

83 changes: 83 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
"strings"
"testing"
@@ -1037,3 +1038,85 @@ func TestHideHelpCommand_WithSubcommands(t *testing.T) {
t.Errorf("Run returned unexpected error: %v", err)
}
}

func TestDefaultCompleteWithFlags(t *testing.T) {
origArgv := os.Args

t.Cleanup(func() {
os.Args = origArgv
})

for _, tc := range []struct {
name string
c *Context
cmd *Command
argv []string
expected string
}{
{
name: "empty",
c: &Context{App: &App{}},
cmd: &Command{},
argv: []string{"prog", "cmd"},
expected: "",
},
{
name: "typical-flag-suggestion",
c: &Context{App: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
&Int64Flag{Name: "everybody-jump-on"},
},
Commands: []*Command{
{Name: "putz"},
},
}},
cmd: &Command{
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
},
argv: []string{"cmd", "--e", "--generate-bash-completion"},
expected: "--excitement\n",
},
{
name: "typical-command-suggestion",
c: &Context{App: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
&Int64Flag{Name: "everybody-jump-on"},
},
}},
cmd: &Command{
Name: "putz",
Subcommands: []*Command{
{Name: "futz"},
},
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
},
argv: []string{"cmd", "--generate-bash-completion"},
expected: "futz\n",
},
} {
t.Run(tc.name, func(ct *testing.T) {
writer := &bytes.Buffer{}
tc.c.App.Writer = writer

os.Args = tc.argv
f := DefaultCompleteWithFlags(tc.cmd)
f(tc.c)

written := writer.String()

if written != tc.expected {
ct.Errorf("written help does not match expected %q != %q", written, tc.expected)
}
})
}
}