-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcontext.go
163 lines (124 loc) · 3.1 KB
/
context.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
package climax
import (
"bytes"
"fmt"
"strings"
)
// Context is a set of arguments and options of command call.
type Context struct {
// Real arguments, excluding any option flags and their values.
//
// Example:
//
// $ app command --force -s="magic" 42 fairy
//
// []string{"42", "fairy"}
//
Args []string
NonVariable map[string]bool
Variable map[string]string
app *Application
}
// Log prints the message to stderrr (each argument takes a distinct line).
func (c *Context) Log(data ...interface{}) {
c.app.Log(data...)
}
// Is returns true if a flag with corresponding name is defined.
func (c *Context) Is(flagName string) bool {
if _, ok := c.NonVariable[flagName]; ok {
return true
}
if _, ok := c.Variable[flagName]; ok {
return true
}
return false
}
// Get returns a value of corresponding variable flag.
// Second (bool) parameter says whether it's really defined or not.
func (c *Context) Get(variableFlagName string) (string, bool) {
value, ok := c.Variable[variableFlagName]
return value, ok
}
func looksLikeFlag(flag string) bool {
if strings.HasPrefix(flag, "-") || strings.HasPrefix(flag, "--") {
return true
}
return false
}
func parseFlagSignature(flag string) (string, string) {
flag = strings.TrimLeft(flag, "-")
equalPos := strings.Index(flag, "=")
if equalPos < 0 {
return flag, ""
}
return flag[:equalPos], flag[equalPos+1:]
}
func flagByName(flags *[]Flag, name string) *Flag {
for i, flag := range *flags {
if flag.Name == name || flag.Short == name {
return &(*flags)[i]
}
}
return nil
}
func newContext(app *Application) *Context {
ctx := Context{}
ctx.Args = []string{}
ctx.NonVariable = make(map[string]bool)
ctx.Variable = make(map[string]string)
ctx.app = app
return &ctx
}
func (a *Application) parseContext(flags []Flag, argv []string) (*Context, error) {
ctx := newContext(a)
for i := 0; i < len(argv); i++ {
argument := argv[i]
if !looksLikeFlag(argument) {
ctx.Args = append(ctx.Args, argument)
continue
}
name, value := parseFlagSignature(argument)
flag := flagByName(&flags, name)
if flag == nil {
return nil, fmt.Errorf(`option -%s does not exist`, name)
}
if flag.Variable {
if value == "" {
if strings.HasSuffix(argument, "=") {
ctx.Variable[flag.Name] = ""
continue
}
if i+1 >= len(argv) {
return nil, fmt.Errorf(`option -%s is invalid`, name)
}
if looksLikeFlag(argv[i+1]) {
return nil, fmt.Errorf(`option -%s is invalid`, name)
}
ctx.Variable[flag.Name] = argv[i+1]
i++
continue
}
ctx.Variable[flag.Name] = value
} else {
if value != "" {
return nil, fmt.Errorf(`-%s is not variable option`, name)
}
ctx.NonVariable[flag.Name] = true
}
}
return ctx, nil
}
func (c Context) String() string {
var b bytes.Buffer
fmt.Fprintf(&b, "Context {\n")
fmt.Fprintf(&b, "\tArgs: %q\n", c.Args)
fmt.Fprintf(&b, "\tFlags:\n")
for flag := range c.NonVariable {
fmt.Fprintf(&b, "\t\t%s\n", flag)
}
for flag, value := range c.Variable {
fmt.Fprintf(&b, "\t\t%s=%s\n", flag, value)
}
fmt.Fprintf(&b, "}")
return b.String()
}