实现命令行参数

Flag

flag是Go语言的一个标准包,用来处理命令行参数。

代码

package main

import "flag"

type CmdLine struct {
    Help bool
    Host string
    MASTER string
    ETCD_ENDPOINTS string
    VERSION string
    FLANNEL_VERSION string
    DNS_IP string
}

var cmdline CmdLine

func init()  {
    flag.BoolVar(&cmdline.Help,"help", false, "show usage")
    flag.StringVar(&cmdline.Host, "host", "10.100.0.1", "KUBERNETES ADD NODE HOST")
    flag.StringVar(&cmdline.MASTER, "master", "http://k8s-master.icarbonx.net:8080", "KUBERNETES K8S_MASTER PATH")
    flag.StringVar(&cmdline.ETCD_ENDPOINTS, "etcd", "http://etcd.icarbonx.net:2379", "KUBERNETES ETCD_ENDPOINTS PATH")
    flag.StringVar(&cmdline.VERSION,"version", "v1.12.3","KUBERNETES VERSION")
    flag.StringVar(&cmdline.DNS_IP, "dns", "172.18.8.8", "KUBERNETES DNS IP")
}

func main() {
    flag.Parse()
    if cmdline.Help {
        flag.Usage()
        return
    }

    println("Help:", cmdline.Help)
    println("Host:", cmdline.Host)
    println("MASTER:", cmdline.MASTER)
    println("ETCD_ENDPOINTS:", cmdline.ETCD_ENDPOINTS)
    println("VERSION:", cmdline.VERSION)
    println("FLANNEL_VERSION:", cmdline.FLANNEL_VERSION)
    println("DNS_IP:", cmdline.DNS_IP)
}

编译创建可执行文件

go build -o commmandk8s commandk8s.go

-o: 使用-o标记可以指定输出文件(可执行文件)的名称

clipboard.png

执行可执行文件

clipboard.png


Anson
0 声望1 粉丝