Skip to content

Instantly share code, notes, and snippets.

@KevinWang15
Created December 30, 2020 07:26
Show Gist options
  • Save KevinWang15/0c16e4ed6a43918bdca2dcb0f94a2297 to your computer and use it in GitHub Desktop.
Save KevinWang15/0c16e4ed6a43918bdca2dcb0f94a2297 to your computer and use it in GitHub Desktop.
glog / klog with cobra
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()
}
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