package cmd import ( "os" "github.com/spf13/cobra" ) var rootCmd = &cobra.Command{ Use: "yr", Short: "yr is CLI tool for weather forecasts", Run: root, } func Execute() { err := rootCmd.Execute() if err != nil { os.Exit(1) } } var ( isJson bool isDebug bool isUTC bool isWeb bool lon float64 lat float64 ) func init() { rootCmd.PersistentFlags().BoolVar(&isJson, "json", false, "Result in JSON") rootCmd.PersistentFlags().BoolVar(&isDebug, "debug", false, "Display debug info") rootCmd.PersistentFlags().BoolVar(&isUTC, "utc", false, "Result times in UTC") rootCmd.PersistentFlags().BoolVar(&isWeb, "web", false, "Open result in browser") rootCmd.PersistentFlags().Float64VarP(&lon, "lon", "x", 0, "Longitude coordinate") rootCmd.PersistentFlags().Float64VarP(&lat, "lat", "y", 0, "Latitude coordinate") } func root(cmd *cobra.Command, args []string) { if isWeb { openBrowser("https://www.yr.no/en", nil) } err := cmd.Help() cobra.CheckErr(err) }