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 @   技术颜良  阅读(111)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2018-03-22 大数据平台搭建(hadoop+spark)
2018-03-22 centos7搭建ELK Cluster集群日志分析平台
点击右上角即可分享
微信分享提示