3.urfave/cli.v2的进阶使用

urfave - cli.v2的进阶使用

Ordering:排序

sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
//命令将排序展示

COMMANDS:
   add, a       add a task to the list
   complete, c  complete a task on the list
   help, h      Shows a list of commands or help for one command
    
GLOBAL OPTIONS:
   --config FILE, -c FILE  Load configuration from FILE
   --lang value, -l value  Language for the greeting (default: "english")
   --help, -h              show help (default: false)

Flags for the application and commands are shown in the order they are defined. However, it's possible to sort them from outside this library by using FlagsByName or CommandsByName with sort.

应用程序和命令的标志按定义的顺序显示。但是,可以通过使用来从这个库之外对它们进行排序FlagsByName or CommandsByName with sort.

For example this:

Flags: []cli.Flag{
        &cli.StringFlag{
            Name:    "lang",
            Aliases: []string{"l"},
            Value:   "english",
            Usage:   "Language for the greeting",
        },
        &cli.StringFlag{
            Name:    "config",
            Aliases: []string{"c"},
            Usage:   "Load configuration from `FILE`",
        },
    },
    Commands: []*cli.Command{
      {
        Name:    "complete",
        Aliases: []string{"c"},
        Usage:   "complete a task on the list",
        Action:  func(c *cli.Context) error {
          return nil
        },
      },
      {
        Name:    "add",
        Aliases: []string{"a"},
        Usage:   "add a task to the list",
        Action:  func(c *cli.Context) error {
          return nil
        },
      },
    },


sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))

Values from the Environment:设置默认值为环境变量值

EnvVars: []string{"APP_LANG"},

如果EnvVars包含多于一个string,则由第一个值决定

EnvVars: []string{"LEGACY_COMPAT_LANG", "APP_LANG", "LANG"},

Values from files:设置默认值为文件值

FilePath: "abc.txt",

示例代码

app.Flags = []cli.Flag{
        &cli.StringFlag{
            Name:     "password",
            Aliases:  []string{"p"},
            Usage:    "password for the mysql database",
            FilePath: "abc.txt",        //abc.txt文件内容为:123
        },
    }
输出: default值为123
GLOBAL OPTIONS:
   --password value, -p value  password for the mysql database (default: "123")
   --help, -h                  show help (default: false)

Required Flags:必须参数

app.Flags = []cli.Flag {
    &cli.StringFlag{
      Name: "lang",
      Value: "english",
      Usage: "language for the greeting",
      Required: true,       //Required设置为true
    },
  }

将Required设置为true,如果在使用命令行的时候没有lang这个flag,则会返回一个错误

2020/11/13 17:42:54 Required flag "lang" not set
exit status 1

Default Values for help output:帮助输出的默认值

DefaultText:"random"    
输出: --port value  Use a randomized port (default: random)
app := &cli.App{
    Flags: []cli.Flag{
      &cli.IntFlag{
        Name:    "port",
        Usage:   "Use a randomized port",
        Value: 0,
        DefaultText: "random",
      },
    },
  }

默认值优先级 由高到低

0.Command line flag value from user     由用户在命令行输入的值
1.Environment variable (if specified)   环境变量(如果指定)
2.Configuration file (if specified)     配置文件(如果指定)
3.Default defined on the flag           Flag上定义的默认值         
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容