Skip to content

Instantly share code, notes, and snippets.

@joerx
Created September 14, 2018 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerx/0b5f08951e363e9556033619f60a71ff to your computer and use it in GitHub Desktop.
Save joerx/0b5f08951e363e9556033619f60a71ff to your computer and use it in GitHub Desktop.
Boolean flags with cobra
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var cfgFile string
var verbose bool
var rootCmd = &cobra.Command{
Use: "cobra",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(verbose) // call main.go --verbose=false or -v=false prints false here
},
}
func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", true, "verbose output")
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment