urfave / cli

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

BoolFlag Count and/or compound not working #1942

Closed native0090 closed 24 minutes ago

native0090 commented 2 weeks ago

My urfave/cli version is

v2.27.2

Dependency Management

Describe the bug

The v2 documentation states:

For bool flags you can specify the flag multiple times to get a count(e.g -v -v -v or -vvv)

It seems that you can specify the flag multiple times with -v -v -v but not with -vvv.

To reproduce

Minimal example to reproduce:

package main

import (
    "fmt"
    "log"
    "os"

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

func main() {
    var count int

    app := &cli.App{
        Flags: []cli.Flag{
            &cli.BoolFlag{
                Name:    "verbosity",
                Aliases: []string{"v"},
                Count:   &count,
            },
        },
        Action: func(cCtx *cli.Context) error {
            fmt.Println("count", count)
            return nil
        },
    }

    if err := app.Run(os.Args); err != nil {
        log.Fatal(err)
    }
}

Observed behavior

Running this program without parameters works as expected. Running with -v works as expected. Running with -v -v works as expected. Running with -vv does not work as expected and prints following message:

2024/07/01 12:17:32 flag provided but not defined: -vv

Run go version and paste its output here

go version go1.22.4 linux/amd64
dearchap commented 2 weeks ago

@native0090 You need to set App.UseShortOptionHandling = true for this to work

https://go.dev/play/p/8o9KPIu8a8x

native0090 commented 2 weeks ago

Thank you, that works! Maybe a hint to this variable in the Flags section would be useful.

dearchap commented 2 weeks ago

@native0090 can you submit a PR with doc changes ?