Skip to content

A BoolWithInverseFlag in a combined short-option token is set to false #2439

Closed

Description

@DRMacIver

My urfave/cli version is

v3.13.0, and main at 4fa44ab.

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

With UseShortOptionHandling, a BoolWithInverseFlag named inside a combined short-option token comes out false: -cv sets color to false, while -c -v sets it to true.

To reproduce

package main

import (
	"context"
	"fmt"
	"os"

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

func main() {
	for _, args := range [][]string{{"app", "-c", "-v"}, {"app", "-cv"}, {"app", "-vc"}} {
		cmd := &cli.Command{
			Name:                   "app",
			UseShortOptionHandling: true,
			Flags: []cli.Flag{
				&cli.BoolWithInverseFlag{Name: "color", Aliases: []string{"c"}},
				&cli.BoolFlag{Name: "verbose", Aliases: []string{"v"}},
			},
			Action: func(_ context.Context, cmd *cli.Command) error {
				fmt.Printf("%v: color=%v verbose=%v\n", args[1:], cmd.Bool("color"), cmd.Bool("verbose"))
				return nil
			},
		}
		if err := cmd.Run(context.Background(), args); err != nil {
			fmt.Fprintln(os.Stderr, err)
		}
	}
}

Observed behavior

[-c -v]: color=true verbose=true
[-cv]: color=false verbose=true
[-vc]: color=false verbose=true

No error is returned, and cmd.IsSet("color") is true in all three cases. The position of the letter in the token makes no difference; the plain BoolFlag in the same token is set correctly.

Expected behavior

-cv and -vc to set color to true, as -c -v does.

Additional context

Tested on v3.13.0 and on current main (4fa44ab), with go1.27.1 darwin/arm64.

BTW, this was found by an automated program that writes property-based tests for various open source projects using hegel (but it has been reviewed by hand before reporting). We've also potentially found (but not yet hand validated) 7 other bugs in cli. You can see the tests at https://github.com/hegeldev/hegel-zoo/tree/main/targets/go/cli. Let us know if you would like us to file the other bugs found and/or contribute the tests. NB the tests are currently LLM generated and probably not yet suitable for inclusion as is, but we're happy to help get them into a better state if you want them.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions