cli
cli copied to clipboard
Parse arguments into struct
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)
}
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.
Closing this as it has become stale.
I like this feature. Would make user code much easier to handle.
ftr some prior work exists that is compatible with urfave/cli v1: https://github.com/octago/sflags/blob/20f2a9c31dfce88b6e1ac276bfd395817089e0a0/examples/urfave_cli/main.go
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