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

Weird behaviour of BoolTFlag compared to BoolFlag #680

Closed
osleg opened this issue Nov 9, 2017 · 4 comments
Closed

Weird behaviour of BoolTFlag compared to BoolFlag #680

osleg opened this issue Nov 9, 2017 · 4 comments

Comments

@osleg
Copy link

osleg commented Nov 9, 2017

Given this snippet

func main() {

	// Setup initial stuff
	cl := cli.NewApp()

	// Define flags
	cl.Flags = []cli.Flag{
		cli.BoolTFlag{
			Name:  "daemon, d",
			Usage: "Controls whether to run in daemon mode",
		},
		cli.BoolFlag{
			Name:  "debug",
			Usage: "Enable debug output",
		},
	}

	cl.Action = func(c *cli.Context) error {
		fmt.Println("Daemon:", c.BoolT("daemon"))
		fmt.Println("Debug", c.Bool("debug"))

		return nil
	}

	cl.Run(os.Args)
}

The output of running the app without args is what I'm expecting

$ go build && ./monitor
Daemon: true
Debug false

But if I provide the flags the --daemon flag is ignored

$ go build && ./monitor --debug --daemon
Daemon: true
Debug true

Though if I provide --daemon=false it works as intended

$ go build && ./monitor --debug --daemon=false
Daemon: false
Debug true

Is this expected behaviour? --debug doesn't require the explicit true and acts like switch

@jszwedko
Copy link
Contributor

jszwedko commented Nov 9, 2017

Hi @osleg,

This is the expected behavior of BoolTFlag. It essentially functions the same as BoolFlag just with a default of true so =false is required for setting the flag to false (in the v2 branch it is actually dropped in lieu of just adding a .Value field to BoolFlag to set the default). You could consider a --no-daemon or --foreground flag if the default behavior is to run as a daemon.

Hope this helps!

@osleg
Copy link
Author

osleg commented Nov 9, 2017

Thanks for answer, thou this is a bit confusing since as it could be seen in the demo above the BoolFlag does not require =true to become truthful. Seems like BoolFlag can act as switch while BoolTFlag can't.
Would be nice to have this specified in docs, not a big issue tho :)

@jszwedko
Copy link
Contributor

👍 I agree it is a bit confusing which is why I believe it's removal in v2 will be an improvement.

It is specified in the godoc for the package (https://godoc.org/github.com/urfave/cli#BoolTFlag), but let me know if you think the documentation there could be improved (or open a PR 😄 ).

Gonna close this out, but thanks for opening this discussion!

@osleg
Copy link
Author

osleg commented Nov 12, 2017

With pleasure, PR added ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants