Skip to content

Shell completion fails for -- and --prefix flag patterns #2315

@morozov

Description

@morozov

My urfave/cli version is

v3 (a9261cf)

Checklist

  • Are you running the latest v3 release? The list of releases is here.
  • Did you check the manual for your release? The v3 manual is here
  • Did you perform a search about this problem? Here's the GitHub guide about searching.

Dependency Management

  • My project is using go modules.

Describe the bug

Shell completion does not show flag suggestions when the user types --<TAB> or --partial<TAB>.

To reproduce

  1. Create a CLI app with shell completion enabled and some flags
  2. Source the shell completion script
  3. Type myapp --<TAB> or myapp --partial<TAB>
package main

import (
	"context"
	"os"

	"github.com/urfave/cli/v3"
)

func main() {
	cmd := &cli.Command{
		Name:                  "myapp",
		EnableShellCompletion: true,
		Flags: []cli.Flag{
			&cli.StringFlag{Name: "config"},
			&cli.BoolFlag{Name: "verbose"},
		},
		Action: func(ctx context.Context, c *cli.Command) error {
			return nil
		},
	}
	cmd.Run(context.Background(), os.Args)
}

Observed behavior

  • myapp --<TAB> produces no completions
  • myapp --prefix<TAB> produces no completions
  • myapp -<TAB> appends another - and produces flag completions (correct)

Expected behavior

  • myapp --<TAB> should show all available flags (--config, --verbose, --help)
  • myapp --prefix<TAB> should show all matching flags

Additional context

The bug has four root causes:

  1. checkShellCompleteFlag() disables completion mode when -- appears anywhere in arguments, but -- is the token being completed
  2. DefaultCompleteWithFlags() returns early when lastArg == "--"
  3. parseFlags() strips -- from args during completion, so lastArg becomes empty
  4. parseFlags() drops unknown/partial flags like --c from posArgs before they reach the completion handler

Want to fix this yourself?

Yes, I have a fix ready.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/v3relates to / is being considered for v3kind/bugdescribes or fixes a bugstatus/triagemaintainers still need to look into this

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions