-
Notifications
You must be signed in to change notification settings - Fork 1
/
flag.go
58 lines (49 loc) · 1.19 KB
/
flag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package istools
import (
"flag"
"fmt"
"strings"
)
// Tagged is just a pair of strings. A values and some associated tag.
type Tagged struct {
Tag string
Value string
}
// TagSlice is a list of tagged options.
type TagSlice []Tagged
// String returns the list of tags.
func (v *TagSlice) String() string {
return fmt.Sprintf("%v", *v)
}
// Set fills up the slice gradually.
func (v *TagSlice) Set(value string) error {
f := taggedFlag{}
if err := f.Set(value); err != nil {
return err
}
*v = append(*v, f.Tagged)
return nil
}
// taggedFlag satisfies the Value interface.
type taggedFlag struct {
Tagged
}
// String returns the tag and value separated by colon.
func (v *taggedFlag) String() string {
return fmt.Sprintf("%s:%s", v.Tag, v.Value)
}
// Set tries to parse the value.
func (v *taggedFlag) Set(s string) error {
var parts = strings.Split(s, ":")
if len(parts) != 2 {
return fmt.Errorf("format must be [TAG]:[PATH]")
}
v.Tagged.Tag, v.Tagged.Value = parts[0], parts[1]
return nil
}
// TaggedFlag can be used for parsing flags.
func TaggedFlag(name string, value Tagged, usage string) *Tagged {
f := taggedFlag{value}
flag.CommandLine.Var(&f, name, usage)
return &f.Tagged
}