forked from symfony-cli/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
219 lines (199 loc) · 5.37 KB
/
command.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* 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 (
"fmt"
"regexp"
"strings"
)
type Alias struct {
Name string
Hidden bool
}
func (a *Alias) String() string {
return a.Name
}
// Command is a subcommand for a console.App.
type Command struct {
// The name of the command
Name string
// A list of aliases for the command
Aliases []*Alias
// A short description of the usage of this command
Usage string
// A longer explanation of how the command works
Description string
// or a function responsible to render the description
DescriptionFunc DescriptionFunc
// The category the command is part of
Category string
// The function to call when checking for shell command completions
ShellComplete ShellCompleteFunc
// An action to execute before any sub-subcommands are run, but after the context is ready
// If a non-nil error is returned, no sub-subcommands are run
Before BeforeFunc
// An action to execute after any subcommands are run, but after the subcommand has finished
// It is run even if Action() panics
After AfterFunc
// The function to call when this command is invoked
Action ActionFunc
// List of flags to parse
Flags []Flag
// List of args to parse
Args ArgDefinition
// Treat all flags as normal arguments if true
FlagParsing FlagParsingMode
// Boolean to hide this command from help
Hidden func() bool
// Full name of command for help, defaults to full command name, including parent commands.
HelpName string
// The name used on the CLI by the user
UserName string
}
func Hide() bool {
return true
}
// FullName returns the full name of the command.
// For subcommands this ensures that parent commands are part of the command path
func (c *Command) FullName() string {
if c.Category != "" {
return strings.Join([]string{c.Category, c.Name}, ":")
}
return c.Name
}
func (c *Command) PreferredName() string {
name := c.FullName()
if name == "" && len(c.Aliases) > 0 {
names := []string{}
for _, a := range c.Aliases {
if name := a.String(); name != "" {
names = append(names, a.String())
}
}
return strings.Join(names, ", ")
}
return name
}
// Run invokes the command given the context, parses ctx.Args() to generate command-specific flags
func (c *Command) Run(ctx *Context) (err error) {
if HelpFlag != nil {
// append help to flags
if !hasFlag(c.Flags, HelpFlag) {
c.Flags = append(c.Flags, HelpFlag)
}
}
set, err := c.parseArgs(ctx.rawArgs().Tail(), ctx.App.FlagEnvPrefix)
context := NewContext(ctx.App, set, ctx)
context.Command = c
if err == nil {
err = checkFlagsValidity(c.Flags, set, context)
}
if err == nil {
err = checkRequiredArgs(c, context)
}
if err != nil {
ShowCommandHelp(ctx, c.FullName())
fmt.Fprintln(ctx.App.Writer)
return IncorrectUsageError{err}
}
if checkCommandHelp(context, c.FullName()) {
return nil
}
if c.After != nil {
defer func() {
afterErr := c.After(context)
if afterErr != nil {
HandleExitCoder(err)
if err != nil {
err = newMultiError(err, afterErr)
} else {
err = afterErr
}
}
}()
}
if c.Before != nil {
err = c.Before(context)
if err != nil {
ShowCommandHelp(ctx, c.FullName())
HandleExitCoder(err)
return err
}
}
err = c.Action(context)
if err != nil {
HandleExitCoder(err)
}
return err
}
// Names returns the names including short names and aliases.
func (c *Command) Names() []string {
names := []string{}
if name := c.FullName(); name != "" {
names = append(names, name)
}
for _, a := range c.Aliases {
if a.Hidden {
continue
}
if name := a.String(); name != "" {
names = append(names, a.String())
}
}
return names
}
// HasName returns true if Command.Name matches given name
func (c *Command) HasName(name string, exact bool) bool {
possibilities := []string{}
if c.Category != "" {
possibilities = append(possibilities, strings.Join([]string{c.Category, c.Name}, ":"))
} else {
possibilities = append(possibilities, c.Name)
}
for _, alias := range c.Aliases {
possibilities = append(possibilities, alias.String())
}
for _, p := range possibilities {
if p == name {
return true
}
}
if exact {
return false
}
parts := strings.Split(name, ":")
for i, part := range parts {
parts[i] = regexp.QuoteMeta(part)
}
re := regexp.MustCompile("^" + strings.Join(parts, "[^:]*:") + "[^:]*$")
for _, p := range possibilities {
if re.MatchString(p) {
return true
}
}
return false
}
// Arguments returns a slice of the Arguments
func (c *Command) Arguments() ArgDefinition {
return ArgDefinition(c.Args)
}
// VisibleFlags returns a slice of the Flags with Hidden=false
func (c *Command) VisibleFlags() []Flag {
return visibleFlags(c.Flags)
}