fix completion of "--"
What type of PR is this?
(REQUIRED)
- bug
What this PR does / why we need it:
(REQUIRED)
Currently "--<TAB>" is completed to "--help," (note the comma!) This PR fixes it so it completed to "--help" without the comma.
Which issue(s) this PR fixes:
(REQUIRED)
Fixes https://github.com/urfave/cli/issues/1993 Reverts https://github.com/urfave/cli/pull/1933
Special notes for your reviewer:
(fill-in or delete this section)
If the approach is accepted it makes sense to fix this in v2 as well.
Testing
(fill-in or delete this section)
You can compile the example given for completion here: https://cli.urfave.org/v3/examples/completions/shell-completions and verify what happens you you type "greet --" and press TAB.
Release Notes
(REQUIRED)
Fixed auto-completion of double dash (--)
-- is often used to separate arguments. I would expect -- to complete to itself in this case.
@abitrolly if -- completes to itself, how to see the list of available flags?
Check how common tools behave when TAB is pressed after --:
$ git clone https://github.com/urfave/cli/ --
--also-filter-submodules --mirror --separate-git-dir=
--bare --no-... --server-option=
--branch= --no-checkout --shallow-exclude=
--bundle-uri= --no-hardlinks --shallow-since=
--checkout --origin= --shallow-submodules
--config= --progress --shared
--depth= --quiet --single-branch
--dissociate --recurse-submodules --sparse
--filter= --reference= --tags
--hardlinks --reference-if-able= --template=
--ipv4 --ref-format= --upload-pack=
--ipv6 --reject-shallow --verbose
--jobs= --remote-submodules
--local --revision=
In zsh completion if urfave/cli is used it even prints a description for each option.
@dearchap I suspect that my fix is not sufficient to completely fix -- case. I noticed that it can run the tool itself if trying to complete a subcommand while specifying flags for the parent command. (I.e. a completion attempt was interpreted as if the current command itself was called.)
I tried to reproduce this in tests and wrote this:
func ExampleCommand_Run_shellComplete_bash_withDoubleDashFlag_Subcommand() {
cmd := &cli.Command{
Name: "greet",
EnableShellCompletion: true,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "other",
Aliases: []string{"o"},
},
&cli.StringFlag{
Name: "xyz",
Aliases: []string{"x"},
},
},
Commands: []*cli.Command{
{
Name: "subcmd",
EnableShellCompletion: true,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "aaa",
},
&cli.StringFlag{
Name: "bbb",
},
},
},
},
}
// Simulate a bash environment and command line arguments
os.Setenv("SHELL", "bash")
os.Args = []string{
"greet", "--other", "1",
"subcmd", "--",
"--generate-shell-completion",
}
_ = cmd.Run(context.Background(), os.Args)
// Output:
// --aaa
// --bbb
// --help
}
This should return the list of options, but it produces the help message instead.
Could you help with this one, please? Maybe I'm missing something.