Skip to content

Commit a7eea0b

Browse files
committed
fix completion of "--"
Use https://cli.urfave.org/v3/examples/completions/shell-completions as a reference for completions. The previous version completed "-" -> "--" -> "--help,", but the added comma was incorrect, as no such flag exists and the tool does not accept it. A similar issue occurred with "--version", which was shown as "--version,". The root cause was that completion was explicitly disabled for this case: $ greet -- --generate-shell-completion As a result, it printed the default help message: > NAME: > greet - A new cli application > > USAGE: > greet [global options] [command [command options]] > > COMMANDS: > add, a add a task to the list > complete, c complete a task on the list > template, t options for task templates > help, h Shows a list of commands or help for one command > > GLOBAL OPTIONS: > --help, -h show help Bash used "--help," as a flag suggestion, which was incorrect. In this commit, completion is enabled for the "-- --generate-shell-completion" case, and a test is added to verify correct behavior. With this change, both "-" and "--" now complete to "--help", which is correct.
1 parent f1ad21b commit a7eea0b

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

examples_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,33 @@ func ExampleCommand_Run_shellComplete_bash_withShortFlag() {
268268
// --help
269269
}
270270

271+
func ExampleCommand_Run_shellComplete_bash_withDoubleDashFlag() {
272+
cmd := &cli.Command{
273+
Name: "greet",
274+
EnableShellCompletion: true,
275+
Flags: []cli.Flag{
276+
&cli.Int64Flag{
277+
Name: "other",
278+
Aliases: []string{"o"},
279+
},
280+
&cli.StringFlag{
281+
Name: "xyz",
282+
Aliases: []string{"x"},
283+
},
284+
},
285+
}
286+
287+
// Simulate a bash environment and command line arguments
288+
os.Setenv("SHELL", "bash")
289+
os.Args = []string{"greet", "--", "--generate-shell-completion"}
290+
291+
_ = cmd.Run(context.Background(), os.Args)
292+
// Output:
293+
// --other
294+
// --xyz
295+
// --help
296+
}
297+
271298
func ExampleCommand_Run_shellComplete_bash_withLongFlag() {
272299
cmd := &cli.Command{
273300
Name: "greet",

help.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,18 @@ func DefaultCompleteWithFlags(ctx context.Context, cmd *Command) {
251251
tracef("running default complete with os.Args flags[%v]", args)
252252
}
253253
argsLen := len(args)
254-
lastArg := ""
254+
var lastArg, veryLastArg string
255255
// parent command will have --generate-shell-completion so we need
256256
// to account for that
257257
if argsLen > 1 {
258258
lastArg = args[argsLen-2]
259+
veryLastArg = args[argsLen-1]
259260
} else if argsLen > 0 {
260261
lastArg = args[argsLen-1]
262+
veryLastArg = args[argsLen-1]
261263
}
262264

263-
if lastArg == "--" {
265+
if lastArg == "--" && veryLastArg != completionFlag {
264266
tracef("No completions due to termination")
265267
return
266268
}
@@ -489,15 +491,6 @@ func checkShellCompleteFlag(c *Command, arguments []string) (bool, []string) {
489491
return false, arguments
490492
}
491493

492-
for _, arg := range arguments {
493-
// If arguments include "--", shell completion is disabled
494-
// because after "--" only positional arguments are accepted.
495-
// https://unix.stackexchange.com/a/11382
496-
if arg == "--" {
497-
return false, arguments[:pos]
498-
}
499-
}
500-
501494
return true, arguments[:pos]
502495
}
503496

help_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
12701270
},
12711271
argv: []string{"cmd", "--e", "--", completionFlag},
12721272
env: map[string]string{"SHELL": "bash"},
1273-
expected: "",
1273+
expected: "--excitement\n--hat-shape\n",
12741274
},
12751275
{
12761276
name: "typical-command-suggestion",
@@ -1822,7 +1822,7 @@ func Test_checkShellCompleteFlag(t *testing.T) {
18221822
cmd: &Command{
18231823
EnableShellCompletion: true,
18241824
},
1825-
wantShellCompletion: false,
1825+
wantShellCompletion: true,
18261826
wantArgs: []string{"--", "foo"},
18271827
},
18281828
{

0 commit comments

Comments
 (0)