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

Passing arg to bool flag breaks other flags #1964

Open
3 tasks done
svartkanin opened this issue Aug 12, 2024 · 6 comments
Open
3 tasks done

Passing arg to bool flag breaks other flags #1964

svartkanin opened this issue Aug 12, 2024 · 6 comments
Labels
area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug status/triage maintainers still need to look into this

Comments

@svartkanin
Copy link

My urfave/cli version is

v2.27.4

Checklist

  • Are you running the latest v2 release? The list of releases is here.
  • Did you check the manual for your release? The v2 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

Using a boolean flag with a argument does not terminate the command.

To reproduce

package main

import (
	"fmt"
	"log"
	"os"

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

func main() {
	app := &cli.App{
		Flags: []cli.Flag{
			&cli.BoolFlag{
				Name: "bool",
			},
			&cli.StringFlag{
				Name: "text",
			},
		},
		Action: func(ctx *cli.Context) error {
			fmt.Println(ctx.String("bool"))
			fmt.Println(ctx.String("text"))
			return nil
		},
	}

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

Observed behavior

When a argument is passed to a bool flag then subsequent flags are not read correctly anymore

$ go run . --bool --text str
true
str

$ go run . --bool wrong --text str
true

Expected behavior

If a argument is passed to a bool flag then the command should fail as that is an invalid input

Run go version and paste its output here

go version go1.22.2 darwin/arm64
@svartkanin svartkanin added area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug status/triage maintainers still need to look into this labels Aug 12, 2024
@svartkanin svartkanin changed the title your bug title goes here Passing arg to bool flag breaks other flags Aug 12, 2024
@dearchap
Copy link
Contributor

@svartkanin The whole point of bool flags is to not pass an arg. Why are you passing an arg for the bool flag in the first place ?

@svartkanin
Copy link
Author

@dearchap I'm aware of that, but if someone accidentally uses the flags this way there will be unintentional behaviour because the parsing is all messed up for the flags coming after as it does not terminate.
How would we verify that the flags were used as intended?

@dearchap
Copy link
Contributor

It's not easy. What happens if 'wrong' is actually defined as a sub command ? We would then try to process it as such thinking that's what the user intended

@svartkanin
Copy link
Author

If there is no subcommand defined with that name and the Args parameter is set to false for the given command this should result in a failure

@autowp
Copy link

autowp commented Aug 16, 2024

Also issue with boolFlag in v3.0.0-alpha9:
--flag=false and --flag false is not the same. First parsed to false, but second to true

--flag=0 and --flag 0 too

@dearchap
Copy link
Contributor

Its not really an issue. Its the design for command line flags in general. As per this https://pkg.go.dev/flag this is the standard format

-flag x  // non-boolean flags only

without that it becomes ambigious. We cannot intend user's intention here.

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

No branches or pull requests

3 participants