-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
241 lines (219 loc) · 6.31 KB
/
parse.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package cli
import (
"errors"
"flag"
"fmt"
"io"
"regexp"
"slices"
"strconv"
"strings"
"github.com/mfridman/xflag"
)
// Parse traverses the command hierarchy and parses arguments. It returns an error if parsing fails
// at any point.
//
// This function is the main entry point for parsing command-line arguments and should be called
// with the root command and the arguments to parse, typically os.Args[1:]. Once parsing is
// complete, the root command is ready to be executed with the [Run] function.
func Parse(root *Command, args []string) error {
if root == nil {
return fmt.Errorf("failed to parse: root command is nil")
}
if err := validateCommands(root, nil); err != nil {
return fmt.Errorf("failed to parse: %w", err)
}
// Initialize or update root state
if root.state == nil {
root.state = &State{
path: []*Command{root},
}
} else {
// Reset command path but preserve other state
root.state.path = []*Command{root}
}
// First split args at the -- delimiter if present
var argsToParse []string
var remainingArgs []string
for i, arg := range args {
if arg == "--" {
argsToParse = args[:i]
remainingArgs = args[i+1:]
break
}
}
if argsToParse == nil {
argsToParse = args
}
current := root
if current.Flags == nil {
current.Flags = flag.NewFlagSet(root.Name, flag.ContinueOnError)
}
var commandChain []*Command
commandChain = append(commandChain, root)
// Create combined flags with all parent flags
combinedFlags := flag.NewFlagSet(root.Name, flag.ContinueOnError)
combinedFlags.SetOutput(io.Discard)
// First pass: process commands and build the flag set
i := 0
for i < len(argsToParse) {
arg := argsToParse[i]
// Skip flags and their values
if strings.HasPrefix(arg, "-") {
// For formats like -flag=x or --flag=x
if strings.Contains(arg, "=") {
i++
continue
}
// Check if this flag expects a value
name := strings.TrimLeft(arg, "-")
if f := current.Flags.Lookup(name); f != nil {
if _, isBool := f.Value.(interface{ IsBoolFlag() bool }); !isBool {
// Skip both flag and its value
i += 2
continue
}
}
i++
continue
}
// Try to traverse to subcommand
if len(current.SubCommands) > 0 {
if sub := current.findSubCommand(arg); sub != nil {
root.state.path = append(slices.Clone(root.state.path), sub)
if sub.Flags == nil {
sub.Flags = flag.NewFlagSet(sub.Name, flag.ContinueOnError)
}
current = sub
commandChain = append(commandChain, sub)
i++
continue
}
return current.formatUnknownCommandError(arg)
}
break
}
current.Flags.Usage = func() { /* suppress default usage */ }
// Add the help check here, after we've found the correct command
hasHelp := false
for _, arg := range argsToParse {
if arg == "-h" || arg == "--h" || arg == "-help" || arg == "--help" {
hasHelp = true
break
}
}
// Add flags in reverse order for proper precedence
for i := len(commandChain) - 1; i >= 0; i-- {
cmd := commandChain[i]
if cmd.Flags != nil {
cmd.Flags.VisitAll(func(f *flag.Flag) {
if combinedFlags.Lookup(f.Name) == nil {
combinedFlags.Var(f.Value, f.Name, f.Usage)
}
})
}
}
// Make sure to return help only after combining all flags, this way we get the full list of
// flags in the help message!
if hasHelp {
return flag.ErrHelp
}
// Let ParseToEnd handle the flag parsing
if err := xflag.ParseToEnd(combinedFlags, argsToParse); err != nil {
return fmt.Errorf("command %q: %w", getCommandPath(root.state.path), err)
}
// Check required flags
var missingFlags []string
for _, cmd := range commandChain {
if len(cmd.FlagsMetadata) > 0 {
for _, flagMetadata := range cmd.FlagsMetadata {
if !flagMetadata.Required {
continue
}
flag := combinedFlags.Lookup(flagMetadata.Name)
if flag == nil {
return fmt.Errorf("command %q: internal error: required flag %s not found in flag set", getCommandPath(root.state.path), formatFlagName(flagMetadata.Name))
}
if _, isBool := flag.Value.(interface{ IsBoolFlag() bool }); isBool {
isSet := false
for _, arg := range argsToParse {
if strings.HasPrefix(arg, "-"+flagMetadata.Name) || strings.HasPrefix(arg, "--"+flagMetadata.Name) {
isSet = true
break
}
}
if !isSet {
missingFlags = append(missingFlags, formatFlagName(flagMetadata.Name))
}
} else if flag.Value.String() == flag.DefValue {
missingFlags = append(missingFlags, formatFlagName(flagMetadata.Name))
}
}
}
}
if len(missingFlags) > 0 {
msg := "required flag"
if len(missingFlags) > 1 {
msg += "s"
}
return fmt.Errorf("command %q: %s %q not set", getCommandPath(root.state.path), msg, strings.Join(missingFlags, ", "))
}
// Skip past command names in remaining args
parsed := combinedFlags.Args()
startIdx := 0
for _, arg := range parsed {
isCommand := false
for _, cmd := range commandChain {
if arg == cmd.Name {
startIdx++
isCommand = true
break
}
}
if !isCommand {
break
}
}
// Combine remaining parsed args and everything after delimiter
var finalArgs []string
if startIdx < len(parsed) {
finalArgs = append(finalArgs, parsed[startIdx:]...)
}
if len(remainingArgs) > 0 {
finalArgs = append(finalArgs, remainingArgs...)
}
root.state.Args = finalArgs
if current.Exec == nil {
return fmt.Errorf("command %q: no exec function defined", getCommandPath(root.state.path))
}
return nil
}
var validNameRegex = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_-]*$`)
func validateName(root *Command) error {
if !validNameRegex.MatchString(root.Name) {
return fmt.Errorf("name must start with a letter and contain only letters, numbers, dashes (-) or underscores (_)")
}
return nil
}
func validateCommands(root *Command, path []string) error {
if root.Name == "" {
if len(path) == 0 {
return errors.New("root command has no name")
}
return fmt.Errorf("subcommand in path [%s] has no name", strings.Join(path, ", "))
}
currentPath := append(path, root.Name)
if err := validateName(root); err != nil {
quoted := make([]string, len(currentPath))
for i, p := range currentPath {
quoted[i] = strconv.Quote(p)
}
return fmt.Errorf("command [%s]: %w", strings.Join(quoted, ", "), err)
}
for _, sub := range root.SubCommands {
if err := validateCommands(sub, currentPath); err != nil {
return err
}
}
return nil
}