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

command flags usage #716

Closed
emicklei opened this issue Feb 21, 2018 · 4 comments
Closed

command flags usage #716

emicklei opened this issue Feb 21, 2018 · 4 comments
Labels
help wanted please help if you can! kind/bug describes or fixes a bug status/confirmed confirmed to be valid, but work has yet to start

Comments

@emicklei
Copy link

I found unexpected behavior when working with command related flags. Perhaps I misunderstand it. I wrote a minimal example.

go run main.go test -s flag -g

and read

2018/02/21 14:29:25 s= false
2018/02/21 14:29:25 g= true

Why is s not set ? or what should the command line be ?

package main

import (
	"log"
	"os"

	"github.com/urfave/cli"
)

func main() {
	app := cli.NewApp()
	app.Commands = []cli.Command{
		{
			Name: "test",
			Flags: []cli.Flag{
				cli.BoolFlag{
					Name: "set, s",
				},
			},
			Subcommands: []cli.Command{
				{
					Name:   "flag",
					Action: cmdTestFlag,
					Flags: []cli.Flag{
						cli.BoolFlag{
							Name: "get, g",
						},
					},
				},
			},
		},
	}
	if err := app.Run(os.Args); err != nil {
		log.Fatalln(err)
	}
}

func cmdTestFlag(c *cli.Context) error {
	log.Println("s=", c.Bool("s"))
	log.Println("g=", c.Bool("g"))
	return nil
}
@meatballhat meatballhat self-assigned this Feb 25, 2018
@daniel-nichter
Copy link

daniel-nichter commented Aug 29, 2018

Had similar issue and c.GlobalBool worked. I can't find mention in the docs but I think each Command has its own context, so s doesn't exist in the (sub)command context, and given,

Bool looks up the value of a local BoolFlag, returns false if not found

accessing the bool var returns false not because it's actually false but because it doesn't exist in the context.

@coilysiren coilysiren added kind/bug describes or fixes a bug help wanted please help if you can! labels Aug 9, 2019
@coilysiren
Copy link
Member

I'm marking this as a bug since it's a bug from a user PoV! From the PoV of the CLI, it'd be nice if all flags were functionally "global" 👍 I would be very interested in reviewing any PRs for this! 🙏

@coilysiren coilysiren added the status/confirmed confirmed to be valid, but work has yet to start label Aug 9, 2019
@coilysiren
Copy link
Member

I've been thinking about this more, and I think we want to achieve this

it'd be nice if all flags were functionally "global"

with a config option, and then make that option the default value in v3. I'm tracking V3 here #833

@coilysiren
Copy link
Member

I'm going to bucket this as a part of potential V3 work, and close this specific issue - but I fully intend on following up on this problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted please help if you can! kind/bug describes or fixes a bug status/confirmed confirmed to be valid, but work has yet to start
Projects
None yet
Development

No branches or pull requests

4 participants