Created
December 30, 2020 07:26
-
-
Save KevinWang15/0c16e4ed6a43918bdca2dcb0f94a2297 to your computer and use it in GitHub Desktop.
glog / klog 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 ( | |
"flag" | |
"fmt" | |
"github.com/golang/glog" | |
"github.com/spf13/cobra" | |
"github.com/spf13/pflag" | |
) | |
var testFlag string | |
var ( | |
RootCmd = &cobra.Command{ | |
Use: "test", | |
Run: func(cmd *cobra.Command, args []string) { | |
glog.V(5).Infof("glog info level") | |
fmt.Printf("value for test: %s\n", testFlag) | |
}, | |
} | |
) | |
func init() { | |
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | |
RootCmd.PersistentFlags().StringVar(&testFlag, "test", "foo", "some flags") | |
} | |
func main() { | |
RootCmd.Execute() | |
} |
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 ( | |
"flag" | |
"fmt" | |
"github.com/spf13/cobra" | |
"github.com/spf13/pflag" | |
"k8s.io/klog" | |
) | |
var testFlag string | |
var ( | |
RootCmd = &cobra.Command{ | |
Use: "test", | |
Run: func(cmd *cobra.Command, args []string) { | |
klog.V(5).Infof("glog info level") | |
fmt.Printf("value for test: %s\n", testFlag) | |
}, | |
} | |
) | |
func init() { | |
klog.InitFlags(nil) | |
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | |
RootCmd.PersistentFlags().StringVar(&testFlag, "test", "foo", "some flags") | |
} | |
func main() { | |
RootCmd.Execute() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment