Skip to content

Instantly share code, notes, and snippets.

@simonswine
Created June 22, 2018 11:05
Show Gist options
  • Save simonswine/fce7844b8e4f4a8a4973e177b31e7466 to your computer and use it in GitHub Desktop.
Save simonswine/fce7844b8e4f4a8a4973e177b31e7466 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "github.com/spf13/cobra"
import "os"
var works bool
func init() {
rootCmd.PersistentFlags().BoolVar(&works, "works", false, "testing flag behaviour")
}
var rootCmd = &cobra.Command{
Use: "flags",
Run: func(cmd *cobra.Command, args []string) {
if !cmd.Flags().Lookup("works").Changed {
fmt.Printf("use default")
} else {
if works {
fmt.Printf("works set to true")
} else {
fmt.Printf("works set to false")
}
}
},
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Sprintf("err=%s", err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment