此代码解析命令行参数。如果我输入“/netstat -c /etc/config -I eth0”,它应该是:“c /etc/config \ni eth0”,但不是。终端输出为:
c 配置文件
c接口
./netstat -c /etc/config -i eth0
c configfile
c interface
代码如下:
package main
import (
"flag"
"fmt"
)
type CmdSt struct {
configPtr string
interfacePtr string
}
var cmdSt CmdSt
func usage() {
cmdSt.configPtr = *flag.String("c", "configfile", "configure file to parse ")
cmdSt.interfacePtr = *flag.String("i", "interface", "capture network interface")
/*
a := flag.String("c", "configfile", "configure file to parse ")
b := flag.String("i", "interface", "capture network interface")
*/
flag.Parse()
fmt.Println("c", cmdSt.configPtr)
fmt.Println("i", cmdSt.interfacePtr)
/*
fmt.Println("c:", *a)
fmt.Println("i:", *b)
*/
}
func main() {
usage()
}