package main import ( "flag" "fmt" ) var ( recusive bool test string level int ) func init() { flag.BoolVar(&recusive,"r",false,"recusive xxx") flag.StringVar(&test,"t","default string","string option") flag.IntVar(&level,"l",1,"level of xxx") //存入变量名 名称 默认值 说明 flag.Parse() // 调用这个解析,参数才能生效 } /* 输入: ./flag_opt.exe -r true -t 333 -l 22 或者 ./flag_opt.exe -r -t 333 -l 22 输出: recusive:true test:default string level:1 */ func main() { fmt.Printf("recusive:%v\n",recusive) fmt.Printf("test:%v\n",test) fmt.Printf("level:%v\n",level) }
输出:
D:\GoPath\src\MyProject\pingguoxueyuan\gostudy\listen14\flag_opt>flag_opt.exe -r true -t 333 -l 22 recusive:true test:default string level:1 D:\GoPath\src\MyProject\pingguoxueyuan\gostudy\listen14\flag_opt>flag_opt.exe -h Usage of flag_opt.exe: -l int level of xxx (default 1) -r recusive xxx -t string string option (default "default string")