cli icon indicating copy to clipboard operation
cli copied to clipboard

Strict flag lookup

Open icholy opened this issue 2 years ago • 4 comments

Checklist

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

What problem does this solve?

I've had several cases where I've made a typo when accessing a flag value via the context.

Solution description

I'd like the library to panic if I access an undefined flag.

func main() {
	app := &cli.App{
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name: "foo",
			},
		},
		Action: func(c *cli.Context) error {
		
			// I want this to panic
			_ = c.String("bar")
			return nil
		},
	}
	app.Run([]string{"test"})
}

icholy avatar Jun 06 '22 16:06 icholy

@icholy you may want to consider using the recently-added (by @toaster 🙇🏼) ability to get the value directly from a given flag

meatballhat avatar Jun 18 '22 13:06 meatballhat

I'd much prefer to keep my existing code and just enable some type of "strict mode" flag.

icholy avatar Jul 12 '22 13:07 icholy

@icholy This would be nice, are you willing to submit a PR ?

dearchap avatar Aug 01 '22 00:08 dearchap

@dearchap if you want to, go for it. Edit see the linked pr.

icholy avatar Aug 01 '22 00:08 icholy

Fixed. .

dearchap avatar Sep 15 '22 17:09 dearchap