cli icon indicating copy to clipboard operation
cli copied to clipboard

Parse arguments into struct

Open wesleimp opened this issue 4 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?

Parse arguments into struct instead of get one by one

Solution description

Implement a Parse function to parse all arguments received into struct.

type Config struct {
	Port         int  `env:"PORT" envDefault:"3000"`
	IsProduction bool `env:"PRODUCTION"`
}

func run(c *cli.Context) {
	cfg := Config{}

	err := c.Parse(&cfg)
        if err != nil{
		fmt.Printf("%+v\n", err)
	}

	fmt.Printf("%+v\n", cfg)
}

wesleimp avatar May 11 '20 19:05 wesleimp

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

stale[bot] avatar Aug 09 '20 21:08 stale[bot]

Closing this as it has become stale.

stale[bot] avatar Sep 09 '20 19:09 stale[bot]

I like this feature. Would make user code much easier to handle.

dearchap avatar Aug 28 '22 13:08 dearchap

ftr some prior work exists that is compatible with urfave/cli v1: https://github.com/octago/sflags/blob/20f2a9c31dfce88b6e1ac276bfd395817089e0a0/examples/urfave_cli/main.go

meatballhat avatar Sep 05 '22 13:09 meatballhat

I think its best left to the user to handle the reflection and create the corresponding flags dynamically instead of cli library imposing the pattern

dearchap avatar Oct 21 '22 19:10 dearchap