k8s源码阅读基础 flag标准库(K8s source code reading basic Flag Standard Library)-其他
k8s源码阅读基础 flag标准库(K8s source code reading basic Flag Standard Library)
参考文档:https://pkg.go.dev/flag@go1.18.2
应用:Package flag implements command-line flag parsing.
示例:util 包
package util
import (
"fmt"
"strings"
)
type StringList []string
func (sl *StringList) String() string {
return fmt.Sprint(*sl)
}
func (sl *StringList) Set(value string) error {
for _, s := range strings.Split(value, ",") {
if len(s) == 0 {
return fmt.Errorf("value should not be an empty string")
}
*sl = append(*sl, s)
}
return nil
}
main 包
package main
import (
"TestGolang/util"
"flag"
"fmt"
)
//注意:port、address、apiPrefix均为指针类型
var (
port = flag.Uint("port", 8080, "The port to listen on. Default 8080.")
address = flag.String("address", "127.0.0.1", "The address on the local server to listen to. Default 127.0.0.1")
apiPrefix = flag.String("api_prefix", "/api/v1beta1", "The prefix for API requests on the server. Default '/api/v1beta1'")
etcdServerList util.StringList
)
func init() {
//Var方法使用指定的名字、使用信息注册一个flag。该flag的类型和值由第一个参数表示,该参数应实现了Value接口。
flag.Var(&etcdServerList, "etcd_servers", "Value type is string; Servers for the etcd (http://ip:port), comma separated")
}
func main() {
//从os.Args[1:]中解析注册的flag。 os.Args []string 保管了命令行参数,第一个是程序名。
flag.Parse()
fmt.Printf("port type is %T \n", port)
fmt.Println("port:", *port)
fmt.Println("address:", *address)
fmt.Println("apiPrefix:", *apiPrefix)
fmt.Println("etcdServerList:", etcdServerList)
}
help 效果:
D:\Projects\GolangProject\src\TestGolang>TestGolang.exe --help
Usage of TestGolang.exe:
-address string
The address on the local server to listen to. Default 127.0.0.1 (default "127.0.0.1")
-api_prefix string
The prefix for API requests on the server. Default '/api/v1beta1' (default "/api/v1beta1")
-etcd_servers value
Value type is string; Servers for the etcd (http://ip:port), comma separated
-port uint
The port to listen on. Default 8080. (default 8080)
参数传递效果:
D:\Projects\GolangProject\src\TestGolang>TestGolang.exe -address "127.0.0.1" -api_prefix "/api/v1" -etcd_servers "http://192.168.1.2,http://192.168.1.3" -port 8090
port type is *uint
port: 8090
address: 127.0.0.1
apiPrefix: /api/v1
etcdServerList: [http://192.168.1.2 http://192.168.1.3]
————————
Reference documents: https://pkg.go.dev/flag @go1. eighteen point two
应用:Package flag implements command-line flag parsing.
Example: util package
package util
import (
"fmt"
"strings"
)
type StringList []string
func (sl *StringList) String() string {
return fmt.Sprint(*sl)
}
func (sl *StringList) Set(value string) error {
for _, s := range strings.Split(value, ",") {
if len(s) == 0 {
return fmt.Errorf("value should not be an empty string")
}
*sl = append(*sl, s)
}
return nil
}
main 包
package main
import (
"TestGolang/util"
"flag"
"fmt"
)
//注意:port、address、apiPrefix均为指针类型
var (
port = flag.Uint("port", 8080, "The port to listen on. Default 8080.")
address = flag.String("address", "127.0.0.1", "The address on the local server to listen to. Default 127.0.0.1")
apiPrefix = flag.String("api_prefix", "/api/v1beta1", "The prefix for API requests on the server. Default '/api/v1beta1'")
etcdServerList util.StringList
)
func init() {
//Var方法使用指定的名字、使用信息注册一个flag。该flag的类型和值由第一个参数表示,该参数应实现了Value接口。
flag.Var(&etcdServerList, "etcd_servers", "Value type is string; Servers for the etcd (http://ip:port), comma separated")
}
func main() {
//从os.Args[1:]中解析注册的flag。 os.Args []string 保管了命令行参数,第一个是程序名。
flag.Parse()
fmt.Printf("port type is %T \n", port)
fmt.Println("port:", *port)
fmt.Println("address:", *address)
fmt.Println("apiPrefix:", *apiPrefix)
fmt.Println("etcdServerList:", etcdServerList)
}
Help effect:
D:\Projects\GolangProject\src\TestGolang>TestGolang.exe --help
Usage of TestGolang.exe:
-address string
The address on the local server to listen to. Default 127.0.0.1 (default "127.0.0.1")
-api_prefix string
The prefix for API requests on the server. Default '/api/v1beta1' (default "/api/v1beta1")
-etcd_servers value
Value type is string; Servers for the etcd (http://ip:port), comma separated
-port uint
The port to listen on. Default 8080. (default 8080)
Parameter transfer effect:
D:\Projects\GolangProject\src\TestGolang>TestGolang.exe -address "127.0.0.1" -api_prefix "/api/v1" -etcd_servers "http://192.168.1.2,http://192.168.1.3" -port 8090
port type is *uint
port: 8090
address: 127.0.0.1
apiPrefix: /api/v1
etcdServerList: [http://192.168.1.2 http://192.168.1.3]