Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,36 @@ func TestApp_OrderOfOperations(t *testing.T) {
app.Commands = oldCommands
}

func TestApp_Run_DoubleDashShellCompletionDoesNotExecuteAction(t *testing.T) {
origArgv := os.Args
t.Cleanup(func() {
os.Args = origArgv
})

var output bytes.Buffer
actionCalled := false

app := &App{
EnableBashCompletion: true,
HideHelp: true,
Writer: &output,
Flags: []Flag{
&BoolFlag{Name: "verbose"},
},
Action: func(c *Context) error {
actionCalled = true
return nil
},
}

os.Args = []string{"command", "--", "--generate-bash-completion"}

err := app.Run(os.Args)
expect(t, err, nil)
expect(t, actionCalled, false)
expect(t, output.String(), "--verbose\n")
}

func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
var subcommandHelpTopics = [][]string{
{"command", "foo", "--help"},
Expand Down
12 changes: 7 additions & 5 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,14 @@ func checkShellCompleteFlag(a *App, arguments []string) (bool, []string) {
return false, arguments
}

for _, arg := range arguments {
// If arguments include "--", shell completion is disabled
// because after "--" only positional arguments are accepted.
// https://unix.stackexchange.com/a/11382
// If arguments include "--" before the token being completed, shell
// completion is disabled because after "--" only positional arguments are
// accepted. If "--" is itself the token being completed, keep completion
// enabled so long-flag suggestions still work.
// https://unix.stackexchange.com/a/11382
for i, arg := range arguments[:pos] {
if arg == "--" {
return false, arguments
return i == pos-1, arguments[:pos]
}
}

Expand Down
11 changes: 10 additions & 1 deletion help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,16 @@ func Test_checkShellCompleteFlag(t *testing.T) {
EnableBashCompletion: true,
},
wantShellCompletion: false,
wantArgs: []string{"--", "foo", "--generate-bash-completion"},
wantArgs: []string{"--", "foo"},
},
{
name: "double dash is the token being completed",
arguments: []string{"foo", "--", "--generate-bash-completion"},
app: &App{
EnableBashCompletion: true,
},
wantShellCompletion: true,
wantArgs: []string{"foo", "--"},
},
{
name: "--generate-bash-completion",
Expand Down
Loading