Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in v3: Count is off by 1 #1930

Closed
Skeeve opened this issue Jun 26, 2024 · 6 comments · Fixed by #1941
Closed

Bug in v3: Count is off by 1 #1930

Skeeve opened this issue Jun 26, 2024 · 6 comments · Fixed by #1941
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this

Comments

@Skeeve
Copy link
Contributor

Skeeve commented Jun 26, 2024

Checklist

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

What problem did you observe?

When using cmd.Count("verbose") on my bool flag, it's value is always one too high

Test Program

package main

import (
	"context"
	"fmt"
	"os"

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

func main() {
	cmd := &cli.Command{
		Name:                   "test",
		Usage:                  "Tool for testing",
		HideHelp:               true,
		UseShortOptionHandling: true,
		Flags: []cli.Flag{
			&cli.BoolFlag{
				Name:    "verbose",
				Aliases: []string{"v"},
				Usage:   "Be verbose",
			},
		},
		Action: tester,
	}

	if err := cmd.Run(context.Background(), []string{"prog", "-v"}); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}


func tester(_ context.Context, cmd *cli.Command) (err error) {
	fmt.Printf(`verbosecount: %d`, cmd.Count("verbose"))
	return
}

Also on playground: https://play.golang.com/p/om6L1TD_6s5

@Skeeve Skeeve added area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this labels Jun 26, 2024
@dearchap
Copy link
Contributor

@Skeeve This looks very much like #1737 which was fixed in v2 but not ported to v3. Think you can make a PR ?

@Skeeve
Copy link
Contributor Author

Skeeve commented Jun 27, 2024

Think you can make a PR ?

I doubt it :(

I THINK the issue might be due to these two lines:

f.count++

and

*b.count = *b.count + 1

But I'm not so deep in understanding the source that I can tell for sure.

I'm also wondering: It seems, ALL flags are counted. Why is Count() only available for BoolfFlag? Sure, it's the only Flag wit Countable set, but what's the purpose hiding this info from the user, when it's already available?

@dearchap
Copy link
Contributor

dearchap commented Jun 27, 2024

No one has requested count for non-bool flags. Not sure when I can get around to a fix though.

@Skeeve
Copy link
Contributor Author

Skeeve commented Jun 28, 2024

No one has requested count for non-bool flags. Not sure when I can get around to a fix though.

That "fix" (if it is a fix) should be easy. Just get rid of "if Countable" in the Count function.

@dearchap
Copy link
Contributor

@Skeeve Would you like to submit a PR with the change ? I'll get glad to review it.

@Skeeve
Copy link
Contributor Author

Skeeve commented Jun 28, 2024

I would like to, but I can't. I have the slight feeling there is more wrong to it, than I first thought.

Here are my observations.

  1. Count() returns the flag's countmember
  2. Flag has baseBoolConfig which has a member Count
  3. This Count is never incremented nor returned. At least I was unable to find it and I assume this should have been done.

I created a test command for counting bools.

I also thought about counting all flags. I think it only makes sense for the …SliceFlags as all the others usually are not used more than once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants