Created
June 22, 2018 11:05
-
-
Save simonswine/fce7844b8e4f4a8a4973e177b31e7466 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
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