gopl-zh.github.com/ch7/ch7-04.md

126 lines
4.7 KiB
Markdown
Raw Normal View History

2015-12-09 07:45:11 +00:00
## 7.4. flag.Value接口
2016-01-04 05:38:37 +00:00
2016-02-15 03:06:34 +00:00
在本章我们会学到另一个标准的接口类型flag.Value是怎么帮助命令行标记定义新的符号的。思考下面这个会休眠特定时间的程序
2016-01-04 05:38:37 +00:00
2018-06-13 07:55:10 +00:00
<u><i>gopl.io/ch7/sleep</i></u>
2016-01-03 14:13:29 +00:00
```go
var period = flag.Duration("period", 1*time.Second, "sleep period")
2015-12-09 07:45:11 +00:00
2016-01-03 14:13:29 +00:00
func main() {
2016-01-04 05:38:37 +00:00
flag.Parse()
fmt.Printf("Sleeping for %v...", *period)
time.Sleep(*period)
fmt.Println()
2016-01-03 14:13:29 +00:00
}
```
2016-01-04 05:38:37 +00:00
2016-02-15 03:06:34 +00:00
在它休眠前它会打印出休眠的时间周期。fmt包调用time.Duration的String方法打印这个时间周期是以用户友好的注解方式而不是一个纳秒数字
2016-01-04 05:38:37 +00:00
2016-01-03 14:13:29 +00:00
```
$ go build gopl.io/ch7/sleep
$ ./sleep
Sleeping for 1s...
```
2016-01-04 05:38:37 +00:00
2016-02-15 03:06:34 +00:00
默认情况下,休眠周期是一秒,但是可以通过 -period 这个命令行标记来控制。flag.Duration函数创建一个time.Duration类型的标记变量并且允许用户通过多种用户友好的方式来设置这个变量的大小这种方式还包括和String方法相同的符号排版形式。这种对称设计使得用户交互良好。
2016-01-04 05:38:37 +00:00
2016-01-03 14:13:29 +00:00
```
$ ./sleep -period 50ms
Sleeping for 50ms...
$ ./sleep -period 2m30s
Sleeping for 2m30s...
$ ./sleep -period 1.5h
Sleeping for 1h30m0s...
$ ./sleep -period "1 day"
invalid value "1 day" for flag -period: time: invalid duration 1 day
```
2016-01-04 05:38:37 +00:00
2016-02-15 03:06:34 +00:00
因为时间周期标记值非常的有用所以这个特性被构建到了flag包中但是我们为我们自己的数据类型定义新的标记符号是简单容易的。我们只需要定义一个实现flag.Value接口的类型如下
2016-01-04 05:38:37 +00:00
2016-01-03 14:13:29 +00:00
```go
package flag
// Value is the interface to the value stored in a flag.
type Value interface {
2016-01-04 05:38:37 +00:00
String() string
Set(string) error
2016-01-03 14:13:29 +00:00
}
```
2016-01-04 05:38:37 +00:00
2019-02-19 08:18:15 +00:00
String方法格式化标记的值用在命令行帮助消息中这样每一个flag.Value也是一个fmt.Stringer。Set方法解析它的字符串参数并且更新标记变量的值。实际上Set方法和String是两个相反的操作所以最好的办法就是对他们使用相同的注解方式。
2016-01-03 14:13:29 +00:00
2018-06-09 16:27:25 +00:00
让我们定义一个允许通过摄氏度或者华氏温度变换的形式指定温度的celsiusFlag类型。注意celsiusFlag内嵌了一个Celsius类型§2.5因此不用实现本身就已经有String方法了。为了实现flag.Value我们只需要定义Set方法
2016-01-04 05:38:37 +00:00
2016-01-21 02:22:10 +00:00
<u><i>gopl.io/ch7/tempconv</i></u>
2016-01-03 14:13:29 +00:00
```go
// *celsiusFlag satisfies the flag.Value interface.
type celsiusFlag struct{ Celsius }
func (f *celsiusFlag) Set(s string) error {
2016-01-04 05:38:37 +00:00
var unit string
var value float64
fmt.Sscanf(s, "%f%s", &value, &unit) // no error check needed
switch unit {
case "C", "°C":
f.Celsius = Celsius(value)
return nil
case "F", "°F":
f.Celsius = FToC(Fahrenheit(value))
return nil
}
return fmt.Errorf("invalid temperature %q", s)
2016-01-03 14:13:29 +00:00
}
```
2016-01-04 05:38:37 +00:00
调用fmt.Sscanf函数从输入s中解析一个浮点数value和一个字符串unit。虽然通常必须检查Sscanf的错误返回但是在这个例子中我们不需要。因为如果有错误发生就没有switch case会匹配到。
2016-01-03 14:13:29 +00:00
2018-05-27 20:51:15 +00:00
下面的CelsiusFlag函数将所有逻辑都封装在一起。它返回一个内嵌在celsiusFlag变量f中的Celsius指针给调用者。Celsius字段是一个会通过Set方法在标记处理的过程中更新的变量。调用Var方法将标记加入应用的命令行标记集合中有异常复杂命令行接口的全局变量flag.CommandLine.Programs可能有几个这个类型的变量。调用Var方法将一个`*celsiusFlag`参数赋值给一个flag.Value参数导致编译器去检查`*celsiusFlag`是否有必须的方法。
2016-01-04 05:38:37 +00:00
2016-01-03 14:13:29 +00:00
```go
// CelsiusFlag defines a Celsius flag with the specified name,
// default value, and usage, and returns the address of the flag variable.
// The flag argument must have a quantity and a unit, e.g., "100C".
func CelsiusFlag(name string, value Celsius, usage string) *Celsius {
2016-01-04 05:38:37 +00:00
f := celsiusFlag{value}
flag.CommandLine.Var(&f, name, usage)
return &f.Celsius
2016-01-03 14:13:29 +00:00
}
```
2016-01-04 05:38:37 +00:00
2016-02-15 03:06:34 +00:00
现在我们可以开始在我们的程序中使用新的标记:
2016-01-04 05:38:37 +00:00
2016-01-21 02:22:10 +00:00
<u><i>gopl.io/ch7/tempflag</i></u>
2016-01-03 14:13:29 +00:00
```go
var temp = tempconv.CelsiusFlag("temp", 20.0, "the temperature")
func main() {
2016-01-04 05:38:37 +00:00
flag.Parse()
fmt.Println(*temp)
2016-01-03 14:13:29 +00:00
}
```
2016-01-04 05:38:37 +00:00
2016-02-15 03:06:34 +00:00
下面是典型的场景:
2016-01-04 05:38:37 +00:00
2016-01-03 14:13:29 +00:00
```
$ go build gopl.io/ch7/tempflag
$ ./tempflag
20°C
$ ./tempflag -temp -18C
-18°C
$ ./tempflag -temp 212°F
100°C
$ ./tempflag -temp 273.15K
invalid value "273.15K" for flag -temp: invalid temperature "273.15K"
Usage of ./tempflag:
2016-01-04 05:38:37 +00:00
-temp value
2016-01-03 14:13:29 +00:00
the temperature (default 20°C)
$ ./tempflag -help
Usage of ./tempflag:
2016-01-04 05:38:37 +00:00
-temp value
2016-01-03 14:13:29 +00:00
the temperature (default 20°C)
```
2016-01-04 05:38:37 +00:00
2016-02-15 03:06:34 +00:00
**练习 7.6** 对tempFlag加入支持开尔文温度。
2016-01-04 05:38:37 +00:00
2016-02-15 03:06:34 +00:00
**练习 7.7** 解释为什么帮助信息在它的默认值是20.0没有包含°C的情况下输出了°C。