urfave / cli

A simple, fast, and fun package for building command line apps in Go
https://cli.urfave.org
MIT License
21.89k stars 1.69k forks source link

More powerful shell completion #1905

Open bartekpacia opened 1 month ago

bartekpacia commented 1 month ago

Checklist

What problems are there currenty?

What problem does this solve?

Currently urfave/cli allows for customizing shell completion only for commands.

The Go module that does the best job at shell completions is cobra. Here are docs for it. urfave/cli is not Cobra and I hope it will never become one – it's much more lightweight, minimal, and fun - but I strongly believe that shell completions are a very useful feature.

Proposed solution

ShellComplete func for flags, for example:

cli.StringFlag{
    Name:    "region-id",
    Aliases: []string{"id"},
    Value:   "",
    Usage:   "region to upload data to",
    ShellComplete: func(ctx context.Context, c *cli.Command) {
        fmt.Println("us-central1")
        fmt.Println("us-east1")
        fmt.Println("eu-west1")
    },
},

Additionally, both ShellComplete on flags and on command should provide the content of the prompt, so the developer can e.g. filter out duplicated completions:

cli.Command{
    Name:      "run",
    ShellComplete: func(ctx context.Context, c *cli.Command, args []string) []string {
        options := []string{"alpha", "bravo", "charlie", "delta"}
        if err != nil {
            return
        }

                completions := []string{}
        for _, option := range options {
            if slices.Contains(args, option) {
                continue
            }

            completions = append(completions, option)
        }

                return completions
    },
}

To avoid reinventing the wheel, completion shell scripts could be taken from Cobra (link). Deep dive & research is required to see if it fits into urfave/cli.

Summary of proposed changes

Describe alternatives you've considered

None.

meatballhat commented 1 month ago

@bartekpacia I love this! Most of my recent programmable completion experience has been with click so the signature changes you are recommending make perfect sense to me.

meatballhat commented 1 month ago

oh, and to answer your question about BoolFlag: idk offhand, so try out an implementation and see how it feels?