解析命令行flag标识
发布者:admin 发表于:417天前 阅读数:544 评论:0

使用flag包可以轻松地将命令行标识参数添加到Go应用程序。它有一些缺点——你往往需要复制很多代码以添加简写版本的标志,并且它们是按帮助提示的字母顺序排序的。 有许多第三方库试图解决这些缺点,但本章将重点关注标准库版本而不是那些第三方库。

实践

1.创建flags.go:

package main

import (
    "flag"
    "fmt"
)

// Config存储接收到的标识
type Config struct {
    subject      string
    isAwesome    bool
    howAwesome   int
    countTheWays CountTheWays
}

// Setup 根据传入的标识初始化配置
func (c *Config) Setup() {
    // 你可以使用这样的方式直接初始化标识:
    // var someVar = flag.String("flag_name", "default_val", "description")
    // 但在实际操作中使用结构体来承载会更好一些

    // 完整版
    flag.StringVar(&c.subject, "subject", "", "subject is a string, it defaults to empty")
    // 简写版
    flag.StringVar(&c.subject, "s", "", "subject is a string, it defaults to empty (shorthand)")

    flag.BoolVar(&c.isAwesome, "isawesome", false, "is it awesome or what?")
    flag.IntVar(&c.howAwesome, "howawesome", 10, "how awesome out of 10?")

    // 自定义变量类型
    flag.Var(&c.countTheWays, "c", "comma separated list of integers")
}

// GetMessage 将所有的内部字段拼接成完整的句子
func (c *Config) GetMessage() string {
    msg := c.subject
    if c.isAwesome {
        msg += " is awesome"
    } else {
        msg += " is NOT awesome"
    }

    msg = fmt.Sprintf("%s with a certainty of %d out of 10. Let me count the ways %s", msg, c.howAwesome, c.countTheWays.String())
    return msg
}

2.建立custom.go:

package main

import (
    "fmt"
    "strconv"
    "strings"
)

// CountTheWays使一个自定义变量类型
// 我们会从标识中读取到它
type CountTheWays []int

// 想要实现自定义类型的flag 必须实现flag.Value接口
// 该接口包含了
// String() string
// Set(string) error
func (c *CountTheWays) String() string {
    result := ""
    for _, v := range *c {
        if len(result) > 0 {
            result += " ... "
        }
        result += fmt.Sprint(v)
    }
    return result
}

func (c *CountTheWays) Set(value string) error {
    values := strings.Split(value, ",")

    for _, v := range values {
        i, err := strconv.Atoi(v)
        if err != nil {
            return err
        }
        *c = append(*c, i)
    }

    return nil
}

3.建立main.go:

package main

import (
    "flag"
    "fmt"
)

func main() {
    // 初始化
    c := Config{}
    c.Setup()

    // 常见的调用方式
    flag.Parse()

    // 将通过命令行输入的flag标识拼接打印
    fmt.Println(c.GetMessage())
}

4.将main.go打包为flags,运行会显示:

is NOT awesome with a certainty of 10 out of 10. Let me count the ways 

5.运行./flags -s会显示:

flag needs an argument: -s
Usage of ./flags:
  -c value
        comma separated list of integers
  -howawesome int
        how awesome out of 10? (default 10)
  -isawesome
        is it awesome or what?
  -s string
        subject is a string, it defaults to empty (shorthand)
  -subject string
        subject is a string, it defaults to empty

6.运行./flags -c 1,2,3 -s aaa会显示:

aaa is NOT awesome with a certainty of 10 out of 10. Let me count the ways 1 ... 2 ... 3

说明

本节演示了flag包的大多数常见用法。涉及对自定义变量类型,各种内置变量,长短标识以及结构映射标识的使用。我们需要main函数以调用flag.Parse()。

在这个示例中你会发现能够自动获取-h以显示包含的标识列表。需要注意的是,可以在没有参数的情况下调用的布尔标识,并且标识的顺序无关紧要。

flag包提供了快速构建命令行应用的方法,在工作中我们可以利用其来指定设置日志级别或根据应用程序的需求引导用户输入。在命令行参数章节中,我们将探索标识集并使用参数在它们之间切换。