Skip to content

Instantly share code, notes, and snippets.

@flowerinthenight
Last active December 1, 2017 05:04
Show Gist options
  • Save flowerinthenight/221e26cea495e5adc3ae6a323b4fbdba to your computer and use it in GitHub Desktop.
Save flowerinthenight/221e26cea495e5adc3ae6a323b4fbdba to your computer and use it in GitHub Desktop.
package main
import (
goflag "flag"
"github.com/golang/glog"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
)
var (
rootCmd = &cobra.Command{
Long: "Use glog with cobra.",
Run: echo,
}
str string
)
func init() {
rootCmd.PersistentFlags().StringVar(&str, "echo", "hello", "echo string")
flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
}
func echo(cmd *cobra.Command, args []string) {
goflag.Parse()
glog.Info("echo (info): ", str)
glog.Warning("echo (warn): ", str)
glog.Error("echo (error): ", str)
}
func main() {
err := rootCmd.Execute()
if err != nil {
glog.Error(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment