【协作式原创】自己动手写docker之urfave cli

Basic

  • 执行顺序
  1. app.Run阻塞
  2. app.Action
  3. app.Run继续

Flag

app.Flags 和命令行参数的区别就是: Flags是指定了参数含义的,顺序可换,另一个Args顺序是约定,顺序不可换,所以尽量用Flag就好。

使用help查看,发现Flags对应GLOBAL OPTIONS:

GLOBAL OPTIONS:
   --flag         enable tty
   --lang value   (default: "english")
   --help, -h     show help
   --version, -v  print the version

Command

  • 使用help查看,可以Command看到对应COMMANDS:
  • 对run命令进行help,可以看到Command的Flag对应OPTIONS:而不是GLOBAL OPTIONS:
USAGE:
   main run [command options] [arguments...]

OPTIONS:
   --it  enable tty
  • 并且全局参数Args会被run命令读取,而不会被Action读取到,说明Args是不会重复读取的,只会被最近的命令所接收到。
(base) 192:mydocker didiyu$ go run main.go arg1 arg2 run arg11 arg12 // 这个命令说明args参数不能同时给main命令和run命令
2020/02/27 17:49:52 Main start!
2020/02/27 17:49:52 Before invoking Run!
2020/02/27 17:49:52 main function args:[arg1 arg2 run arg11 arg12] // 全部被当成main命令的参数
2020/02/27 17:49:52 main function sleep 0
2020/02/27 17:49:53 main function sleep 1
2020/02/27 17:49:54 main function sleep 2

os.exec包下的Run和Start两种启动方式

https://godoc.org/os/exec#Cmd.Run

Run = Start + Wait

  • The returned error is nil if the command runs, has no problems copying stdin, stdout, and stderr, and exits with a zero exit status.

    Start

    Wait

猜你喜欢

转载自www.cnblogs.com/yudidi/p/12372516.html
CLI