go flags学习

参考:
https://github.com/fanux/sealos/blob/master/cmd/init.go
https://blog.51cto.com/u_15081058/2594831

package main


import (
"fmt"
"github.com/spf13/cobra"
"os"
)
var initCmd = &cobra.Command{
Use: "init",
Run: func(test101 *cobra.Command, args []string) {
fmt.Println(s)

},
}

var s []string
var test= []string{"che","yunhua"}
func init() {
rootCmd.AddCommand(initCmd)
initCmd.Flags().StringSliceVarP(&s,"master","m",test,"plase is a string slinc")


}
var rootCmd = &cobra.Command{
Use: "test101",
Short: "simplest way install kubernetes tools.",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func main() {
Execute()

}
posted @ 2022-03-22 10:17  技术颜良  阅读(108)  评论(0编辑  收藏  举报