forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
167 lines (145 loc) · 3.38 KB
/
flags.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package flags
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/cloudfoundry/cli/flags/flag"
)
/**
* In main:
* - get os args
* - new cmdContext obj
* - range over os.Args, look for '-'/'--', call flags.Parse() on each of them
* - cmdContext obj populated with flags, cmd now has c.Bool(),c.Int(),c.String() etc...
* - now populate cmdContext with args, anything w/o '-'/'--', now cmd has c.Args()
*
* now run cmd, pass Context obj over
* **/
type FlagSet interface {
fmt.Stringer
GetName() string
GetValue() interface{}
Set(string)
}
type FlagContext interface {
Parse(...string) error
Args() []string
Int(string) int
Bool(string) bool
String(string) string
IsSet(string) bool
}
type flagContext struct {
flagsets map[string]FlagSet
args []string
cmdFlags map[string]FlagSet //valid flags for command
cursor int
}
func NewFlagContext(cmdFlags map[string]FlagSet) FlagContext {
return &flagContext{
flagsets: make(map[string]FlagSet),
cmdFlags: cmdFlags,
cursor: 0,
}
}
func (c *flagContext) Parse(args ...string) error {
var flagset FlagSet
var ok bool
var v string
var err error
for c.cursor <= len(args)-1 {
arg := args[c.cursor]
if strings.HasPrefix(arg, "-") || strings.HasPrefix(arg, "--") {
flg := strings.TrimLeft(strings.TrimLeft(arg, "-"), "-")
if strings.Contains(flg, "=") {
tmpAry := strings.SplitN(flg, "=", 2)
flg = tmpAry[0]
tmpArg := append(args[:c.cursor], tmpAry[1])
args = append(tmpArg, args[c.cursor:]...)
}
if flagset, ok = c.cmdFlags[flg]; !ok {
return errors.New("Invalid flag: " + arg)
}
switch flagset.GetValue().(type) {
case bool:
c.flagsets[flg] = &cliFlags.BoolFlag{Name: flg, Value: c.getBoolFlagValue(args)}
case int:
if v, err = c.getFlagValue(args); err != nil {
return err
}
i, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return errors.New("Value for flag '" + flg + "' must be integer")
}
c.flagsets[flg] = &cliFlags.IntFlag{Name: flg, Value: int(i)}
case string:
if v, err = c.getFlagValue(args); err != nil {
return err
}
c.flagsets[flg] = &cliFlags.StringFlag{Name: flg, Value: v}
}
} else {
c.args = append(c.args, args[c.cursor])
}
c.cursor++
}
return nil
}
func (c *flagContext) getFlagValue(args []string) (string, error) {
if c.cursor >= len(args)-1 {
return "", errors.New("No value provided for flag: " + args[c.cursor])
}
c.cursor++
return args[c.cursor], nil
}
func (c *flagContext) getBoolFlagValue(args []string) bool {
if c.cursor >= len(args)-1 {
return true
}
b, err := strconv.ParseBool(args[c.cursor+1])
if err == nil {
c.cursor++
return b
}
return true
}
func (c *flagContext) Args() []string {
return c.args
}
func (c *flagContext) IsSet(k string) bool {
if _, ok := c.flagsets[k]; ok {
return true
}
return false
}
func (c *flagContext) Int(k string) int {
if _, ok := c.flagsets[k]; ok {
v := c.flagsets[k].GetValue()
switch v.(type) {
case int:
return v.(int)
}
}
return 0
}
func (c *flagContext) String(k string) string {
if _, ok := c.flagsets[k]; ok {
v := c.flagsets[k].GetValue()
switch v.(type) {
case string:
return v.(string)
}
}
return ""
}
func (c *flagContext) Bool(k string) bool {
if _, ok := c.flagsets[k]; ok {
v := c.flagsets[k].GetValue()
switch v.(type) {
case bool:
return v.(bool)
}
}
return false
}