-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontext.go
182 lines (155 loc) · 3.94 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (c) 2021-present Fabien Potencier <[email protected]>
*
* This file is part of Symfony CLI project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package console
import (
"flag"
"fmt"
)
// Context is a type that is passed through to
// each Handler action in a cli application. Context
// can be used to retrieve context-specific args and
// parsed command-line options.
type Context struct {
App *Application
Command *Command
flagSet *flag.FlagSet
args *args
parentContext *Context
}
// NewContext creates a new context. For use in when invoking an App or Command action.
func NewContext(app *Application, set *flag.FlagSet, parentCtx *Context) *Context {
return &Context{App: app, flagSet: set, parentContext: parentCtx}
}
// Set assigns a value to a context flag.
func (c *Context) Set(name, value string) error {
if fs := lookupFlagSet(name, c); fs != nil {
return fs.Set(name, value)
}
return fmt.Errorf("no such flag -%v", name)
}
// IsSet determines if the flag was actually set
func (c *Context) IsSet(name string) bool {
if fs := lookupFlagSet(name, c); fs != nil {
isSet := false
fs.Visit(func(f *flag.Flag) {
if f.Name == name {
isSet = true
}
})
if isSet {
return true
}
}
return false
}
// HasFlag determines if a flag is defined in this context and all of its parent
// contexts.
func (c *Context) HasFlag(name string) bool {
return lookupFlag(name, c) != nil
}
// Lineage returns *this* context and all of its ancestor contexts in order from
// child to parent
func (c *Context) Lineage() []*Context {
lineage := []*Context{}
for cur := c; cur != nil; cur = cur.parentContext {
lineage = append(lineage, cur)
}
return lineage
}
// Args returns the command line arguments associated with the context.
func (c *Context) rawArgs() Args {
v := args{
values: c.flagSet.Args(),
}
return &v
}
func (c *Context) Args() Args {
// cache args fetch
if c.args != nil {
return c.args
}
argsValue := make([]string, 0, c.flagSet.NArg())
for _, arg := range c.flagSet.Args() {
if arg == "--" {
continue
}
argsValue = append(argsValue, arg)
}
c.args = &args{
values: argsValue,
command: c.Command,
}
return c.args
}
// NArg returns the number of the command line arguments.
func (c *Context) NArg() int {
return c.Args().Len()
}
func lookupFlag(name string, ctx *Context) Flag {
for _, c := range ctx.Lineage() {
if c.Command == nil {
continue
}
for _, f := range c.Command.Flags {
for _, n := range f.Names() {
if n == name {
return f
}
}
}
}
if ctx.App != nil {
for _, f := range ctx.App.Flags {
for _, n := range f.Names() {
if n == name {
return f
}
}
}
}
return nil
}
func lookupFlagSet(name string, ctx *Context) *flag.FlagSet {
for _, c := range ctx.Lineage() {
if c.Command != nil {
name = expandShortcut(c.Command.Flags, name)
}
if c.App != nil {
name = expandShortcut(c.App.Flags, name)
}
if f := c.flagSet.Lookup(name); f != nil {
return c.flagSet
}
}
return nil
}
func lookupRawFlag(name string, ctx *Context) *flag.Flag {
for _, c := range ctx.Lineage() {
if c.Command != nil {
name = expandShortcut(c.Command.Flags, name)
}
if c.App != nil {
name = expandShortcut(c.App.Flags, name)
}
if f := c.flagSet.Lookup(name); f != nil {
return f
}
}
return nil
}