Skip to content

Conversation

@starius
Copy link

@starius starius commented Oct 6, 2025

What type of PR is this?

(REQUIRED)

  • bug

What this PR does / why we need it:

(REQUIRED)

Currently "--" 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 #1993
Reverts #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 (--)

@starius starius requested a review from a team as a code owner October 6, 2025 21:46
@starius starius force-pushed the double-dash-completion-fix branch from a7eea0b to c06c926 Compare October 6, 2025 22:04
@starius starius marked this pull request as draft October 6, 2025 22:39
@starius starius force-pushed the double-dash-completion-fix branch 2 times, most recently from 6c066ff to 84addde Compare October 6, 2025 23:32
@starius starius marked this pull request as ready for review October 6, 2025 23:40
@abitrolly
Copy link
Contributor

-- is often used to separate arguments. I would expect -- to complete to itself in this case.

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.
@starius starius force-pushed the double-dash-completion-fix branch from 84addde to f757d4d Compare October 14, 2025 02:19
@starius
Copy link
Author

starius commented Oct 14, 2025

@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.

@starius
Copy link
Author

starius commented Oct 14, 2025

@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.

@starius starius requested a review from dearchap October 14, 2025 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autocomplete after double dash (--) executing command action.

3 participants