24.1命令行参数
os.Args命令行参数的切片
1: func main() {2: name := "Alice"3: fmt.Println("Good Morning", name)4: // 说明用户传入了参数5: if len(os.Args) > 1 {6: for index, v := range os.Args{7: if index != 0 { // index=0的命令行参数是文件路径8: // 打印每个参数9: fmt.Printf("args[%d]=%v\n", index, v)10: }11: }12: }13: }
使用flag包获取命令行参数
1: var recusive bool2: var test string3: var level int4:5: func init() {6: flag.BoolVar(&recusive, "r", false, "recusive xxx")7: flag.StringVar(&test, "t", "defalut string", "string option")8: flag.IntVar(&level, "l", 1, "level of xxxx")9: flag.Parse()10: }11:12: func main() {13: fmt.Printf("recusive:%v\n", recusive)14: fmt.Printf("test:%v\n", test)15: fmt.Printf("level:%v\n", level)16: }
终端运行
1: test.exe -r -t hello -l 8888
运行结果
1: recusive:true2: test:hello3: level:8888
24.2 cli框架
首先使用命令安装cli,go get github.com/urfave/cil
1.基本使用
Action里面的就是业务逻辑,自动封装了-help -version等方法
1: package main2: import (3: "fmt"4: "github.com/urfave/cli"5: "log"6: "os"7: )8: func main() {9: app := cli.NewApp()10: app.Name = "boom"11: app.Usage = "make an explosive entrance"12: app.Action = func(c *cli.Context) error {13: fmt.Println("boom! I say!")14: return nil15: }16:17: err := app.Run(os.Args)18: if err != nil {19: log.Fatal(err)20: }21: }
2.处理args第一个参数
比如输入参数alex,返回 hello "alex!"
1: package main2:3: import (4: "fmt"5: "github.com/urfave/cli"6: "log"7: "os"8: )9:10: func main() {11: app := cli.NewApp()12:13: app.Action = func(c *cli.Context) error {14: //获取第一个参数15: fmt.Printf("Hello %q", c.Args().Get(0))16: return nil17: }18:19: err := app.Run(os.Args)20: if err != nil {21: log.Fatal(err)22: }23: }
3.Flag
1: package main2:3: import (4: "fmt"5: "github.com/urfave/cli"6: "log"7: "os"8: )9:10: func main() {11: app := cli.NewApp()12:13: app.Flags = []cli.Flag{14: cli.StringFlag{15: Name: "lang",16: Value: "english",17: Usage: "language for the greeting",18: },19: }20:21: app.Action = func(c *cli.Context) error {22: name := "Nefertiti"23: if c.NArg() > 0 {24: name = c.Args().Get(0)25: }26: if c.String("lang") == "spanish" {27: fmt.Println("Hola", name)28: } else {29: fmt.Println("Hello", name)30: }31: return nil32: }33:34: err := app.Run(os.Args)35: if err != nil {36: log.Fatal(err)37: }38: }
1: 输入参数alex -lang spanish2: 结果 Hello alex
4.关于commond和subcommand
通过定义这些,我们能实现类似可执行程序 命令 子命令的操作,比如 App.go add 123 控制台输出 added task: 123
1: package main2:3: import (4: "fmt"5: "log"6: "os"7: "github.com/urfave/cli"8: )9:10: func main() {11: app := cli.NewApp()12:13: app.Commands = []cli.Command{14: {15: Name: "add",16: Aliases: []string{"a"},17: Usage: "add a task to the list",18: Action: func(c *cli.Context) error {19: fmt.Println("added task: ", c.Args().First())20: return nil21: },22: },23: {24: Name: "complete",25: Aliases: []string{"c"},26: Usage: "complete a task on the list",27: Action: func(c *cli.Context) error {28: fmt.Println("completed task: ", c.Args().First())29: return nil30: },31: },32: {33: Name: "template",34: Aliases: []string{"t"},35: Usage: "options for task templates",36: Subcommands: []cli.Command{37: {38: Name: "add",39: Usage: "add a new template",40: Action: func(c *cli.Context) error {41: fmt.Println("new task template: ", c.Args().First())42: return nil43: },44: },45: {46: Name: "remove",47: Usage: "remove an existing template",48: Action: func(c *cli.Context) error {49: fmt.Println("removed task template: ", c.Args().First())50: return nil51: },52: },53: },54: },55: }56:57: err := app.Run(os.Args)58: if err != nil {59: log.Fatal(err)60: }61: }62:
类似MYSQL,FTP,TELNET等工具,很多控制台程序,都是进入类似一个自己的运行界面,这样其实CLI本身因为并没有中断,可以保存先前操作的信息。
所以,我们往往只需要用户登陆一次,就可以继续执行上传下载查询通讯等等的后续操作。
代码省略。