I'm following the guide on https://github.com/spf13/cobra#flags, but I'm confused by some of the content there.
I have a few services (rest api, email service, events) and I'm trying to do something like this:
go run *.go rest -env DEV -p 3000
go run *.go events -env DEV -p 3001
I'm following the github page, so I have defined my rootCmd and restCmd as such:
var rootCmd = &cobra.Command{
Use: "myappname",
}
var restCmd = &cobra.Command{
Use: "rest",
Short: "REST API",
Long: "REST API",
Run: runRest,
}
And in the runRest
method, should I be doing something like
var env string
restCmd.Flags().StringVarP(&env, "env", "env", "", "environment")
Please let me know.
Thanks
Each sub command can have their own flags. You can do this as following: