Created
September 14, 2018 05:45
-
-
Save joerx/0b5f08951e363e9556033619f60a71ff to your computer and use it in GitHub Desktop.
Boolean flags with cobra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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