-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
487 lines (428 loc) · 11.9 KB
/
Copy pathcommand.go
File metadata and controls
487 lines (428 loc) · 11.9 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package tinyflags
import (
"context"
"errors"
"fmt"
"io"
"strings"
"github.com/containeroo/tinyflags/internal/core"
)
// Runnable represents a parsed command that can execute with cancellation support.
type Runnable interface {
Run(context.Context) error
}
// Runner is the preferred name for one parsed command executor.
type Runner = Runnable
// Command represents a command or subcommand with local and persistent flags.
type Command struct {
*FlagSet
name string
summary string
handling ErrorHandling
requireChild bool
parent *Command
globals *FlagSet
children map[string]*Command
order []*Command
selected *Command
builder commandBuilder
}
type commandBuilder func() (Runnable, error)
// NewCommand creates a new root command.
func NewCommand(name string, handling ErrorHandling) *Command {
local := NewFlagSet(name, handling)
return &Command{
FlagSet: local,
name: name,
handling: handling,
globals: local,
children: make(map[string]*Command),
}
}
// Command creates a nested subcommand with the given name and summary.
func (c *Command) Command(name string, summary string) *Command {
fullName := c.FullName() + " " + name
child := &Command{
FlagSet: NewFlagSet(fullName, c.handling),
name: name,
summary: summary,
handling: c.handling,
parent: c,
globals: NewFlagSet(fullName, c.handling),
children: make(map[string]*Command),
}
c.children[name] = child
c.order = append(c.order, child)
return child
}
// Globals returns the persistent flag set for this command subtree.
func (c *Command) Globals() *FlagSet {
return c.globals
}
// HelpText renders help for the selected command when available, otherwise the receiver.
func (c *Command) HelpText() string {
target := c
if c != nil && c.selected != nil {
target = c.selected
}
if target == nil {
return ""
}
return renderCommandHelp(target)
}
// WriteHelp writes the rendered help text to w.
func (c *Command) WriteHelp(w io.Writer) error {
if w == nil {
return nil
}
_, err := io.WriteString(w, c.HelpText())
return err
}
// RequireCommand enforces that one direct or nested child command must be selected.
func (c *Command) RequireCommand() *Command {
c.requireChild = true
return c
}
// BuildCommand registers a zero-argument builder that returns the runnable for this command.
func (c *Command) BuildCommand(builder any) *Command {
c.setBuilder(wrapCommandBuilder(builder))
return c
}
// Run registers one handler function plus deferred flag-backed arguments for this command.
func (c *Command) Run(handler any, bindings ...any) *Command {
c.setBuilder(wrapCommandRunner(handler, bindings...))
return c
}
// SelectedCommand returns the leaf command selected during the last parse.
func (c *Command) SelectedCommand() *Command {
return c.selected
}
// Commands returns child commands in registration order.
func (c *Command) Commands() []*Command {
return c.order
}
// Summary returns the short summary used in command listings.
func (c *Command) Summary() string {
return c.summary
}
// Name returns the command segment name.
func (c *Command) Name() string {
return c.name
}
// FullName returns the full command path.
func (c *Command) FullName() string {
if c.parent == nil {
return c.name
}
return c.parent.FullName() + " " + c.name
}
// Parse selects a command path and parses matching local and persistent flags.
func (c *Command) Parse(args []string) error {
c.selected = c
state := commandParseState{
argsBySet: make(map[*FlagSet][]string),
}
current := c
for i := 0; i < len(args); i++ {
arg := args[i]
if arg == "--help" || arg == "-h" {
state.helpTarget = current
continue
}
if arg == "--version" {
state.versionRequested = true
continue
}
if child, ok := current.children[arg]; ok && !strings.HasPrefix(arg, "-") {
// Bare child names advance the command cursor instead of becoming positionals.
current = child
continue
}
if strings.HasPrefix(arg, "--") && arg != "--" {
owner, flag := current.resolveLongArg(arg)
if owner == nil || flag == nil {
state.append(ownerOrCurrent(owner, current), arg)
continue
}
state.append(owner, arg)
// Route the following token with the same owner when the flag consumes a value.
if !strings.Contains(arg, "=") && flagConsumesValue(flag) && i+1 < len(args) && !strings.HasPrefix(args[i+1], "-") {
i++
state.append(owner, args[i])
}
continue
}
if strings.HasPrefix(arg, "-") && arg != "-" {
consumed := current.routeShortArgs(arg, args, &i, &state)
if consumed {
continue
}
}
state.append(current.FlagSet, arg)
}
c.selected = current
if state.versionRequested {
return c.FlagSet.Parse([]string{"--version"})
}
if state.helpTarget != nil {
return RequestHelp(renderCommandHelp(state.helpTarget))
}
var errs []error
for _, cmd := range c.commandPathTo(current) {
for _, fs := range cmd.parseScopes() {
if err := fs.Parse(state.argsBySet[fs]); err != nil {
errs = append(errs, err)
if c.handling != ContinueOnError {
return err
}
}
}
}
if err := c.missingRequiredCommand(current); err != nil {
errs = append(errs, err)
if c.handling != ContinueOnError {
return err
}
}
if len(errs) > 0 {
return errors.Join(errs...)
}
return nil
}
// ParseRunnable parses args and builds the runnable for the selected command.
func (c *Command) ParseRunnable(args []string) (Runnable, error) {
return c.ParseRunner(args)
}
// ParseRunner parses args and builds the runner for the selected command.
func (c *Command) ParseRunner(args []string) (Runner, error) {
if err := c.Parse(args); err != nil {
return nil, err
}
selected := c.SelectedCommand()
if selected == nil {
return nil, fmt.Errorf("tinyflags: no command selected")
}
if selected.builder == nil {
return nil, fmt.Errorf("tinyflags: no command runner registered for command %q", selected.FullName())
}
return selected.builder()
}
type commandParseState struct {
argsBySet map[*FlagSet][]string
helpTarget *Command
versionRequested bool
}
// setBuilder stores the internal runner builder shared by all registration styles.
func (c *Command) setBuilder(builder commandBuilder) {
c.builder = builder
}
// append records one routed argument for a specific flag set.
func (s *commandParseState) append(fs *FlagSet, arg string) {
if fs == nil {
return
}
s.argsBySet[fs] = append(s.argsBySet[fs], arg)
}
// parseScopes returns the flag sets that must parse for this command node.
func (c *Command) parseScopes() []*FlagSet {
if c.parent == nil || c.globals == c.FlagSet {
return []*FlagSet{c.FlagSet}
}
return []*FlagSet{c.globals, c.FlagSet}
}
// commandPathTo returns the command chain from the receiver to the target.
func (c *Command) commandPathTo(target *Command) []*Command {
var path []*Command
for cur := target; cur != nil; cur = cur.parent {
path = append(path, cur)
if cur == c {
break
}
}
for i, j := 0, len(path)-1; i < j; i, j = i+1, j-1 {
path[i], path[j] = path[j], path[i]
}
return path
}
func (c *Command) missingRequiredCommand(selected *Command) error {
for _, cmd := range c.commandPathTo(selected) {
if cmd == nil || !cmd.requireChild {
continue
}
if cmd == selected && len(cmd.order) > 0 {
return &UsageError{
Err: &CommandRequired{Command: cmd.FullName()},
Help: renderCommandHelp(cmd),
}
}
}
return nil
}
// availableFlagSets returns local and inherited persistent flag sets in lookup order.
func (c *Command) availableFlagSets() []*FlagSet {
scopes := []*FlagSet{c.FlagSet}
if c.parent != nil && c.globals != c.FlagSet {
scopes = append(scopes, c.globals)
}
for parent := c.parent; parent != nil; parent = parent.parent {
if parent.globals != nil {
scopes = append(scopes, parent.globals)
}
}
return scopes
}
// resolveLongArg finds which flag set owns a long-form flag token.
func (c *Command) resolveLongArg(arg string) (*FlagSet, *core.BaseFlag) {
name := strings.TrimPrefix(arg, "--")
if eq := strings.Index(name, "="); eq >= 0 {
name = name[:eq]
}
parts := strings.Split(name, ".")
for _, fs := range c.availableFlagSets() {
if fl := fs.impl.LookupFlag(name); fl != nil {
return fs, fl
}
if len(parts) == 3 {
if fl := lookupDynamicFlag(fs, parts[0], parts[2]); fl != nil {
return fs, fl
}
}
}
return nil, nil
}
// resolveShortFlag finds which flag set owns a short-form flag token.
func (c *Command) resolveShortFlag(short string) (*FlagSet, *core.BaseFlag) {
for _, fs := range c.availableFlagSets() {
for _, fl := range fs.impl.OrderedStaticFlags() {
if fl.Short == short {
return fs, fl
}
}
}
return nil, nil
}
// routeShortArgs routes grouped short options to the owning flag sets.
func (c *Command) routeShortArgs(arg string, args []string, i *int, state *commandParseState) bool {
shorts := strings.TrimPrefix(arg, "-")
if shorts == "" {
return false
}
for idx := 0; idx < len(shorts); idx++ {
short := string(shorts[idx])
owner, fl := c.resolveShortFlag(short)
if owner == nil || fl == nil {
state.append(c.FlagSet, arg)
return true
}
if flagConsumesValue(fl) {
if idx < len(shorts)-1 {
state.append(owner, "-"+short+shorts[idx+1:])
return true
}
state.append(owner, "-"+short)
if *i+1 < len(args) && !strings.HasPrefix(args[*i+1], "-") {
*i++
state.append(owner, args[*i])
}
return true
}
state.append(owner, "-"+short)
}
return true
}
// flagConsumesValue reports whether a flag expects a following value token.
func flagConsumesValue(fl *core.BaseFlag) bool {
if fl == nil || fl.Value == nil {
return false
}
if _, ok := fl.Value.(core.Incrementable); ok {
return false
}
if b, ok := fl.Value.(core.StrictBool); ok && !b.IsStrictBool() {
return false
}
return true
}
// lookupDynamicFlag resolves a dynamic field inside one dynamic group.
func lookupDynamicFlag(fs *FlagSet, groupName, field string) *core.BaseFlag {
for _, group := range fs.impl.DynamicGroups() {
if group.Name() != groupName {
continue
}
return group.LookupFlag(field)
}
return nil
}
// ownerOrCurrent falls back to the current command's local flag set when unset.
func ownerOrCurrent(owner *FlagSet, current *Command) *FlagSet {
if owner != nil {
return owner
}
return current.FlagSet
}
// renderCommandHelp renders local help plus any child command listing.
func renderCommandHelp(cmd *Command) string {
helpText := renderLocalHelp(cmd.FlagSet)
lines := strings.Split(strings.TrimRight(helpText, "\n"), "\n")
if len(cmd.order) > 0 && len(lines) > 0 {
lines[0] = renderUsageLine(cmd)
}
var b strings.Builder
b.WriteString(strings.Join(lines, "\n"))
if len(cmd.order) > 0 {
b.WriteString("\n\nCommands:\n")
width := longestCommandName(cmd.order)
for _, child := range cmd.order {
fmt.Fprintf(&b, " %-*s %s\n", width, child.name, child.summary)
}
}
b.WriteString("\n")
return b.String()
}
// renderLocalHelp renders help text for one command-local flag set.
func renderLocalHelp(fs *FlagSet) string {
if fs == nil || fs.impl == nil {
return ""
}
return fs.impl.RenderHelpText()
}
// renderUsageLine builds the usage line for a command help screen.
func renderUsageLine(cmd *Command) string {
usage := "Usage: " + cmd.FullName()
if hasAnyVisibleFlags(cmd.FlagSet) {
usage += " [flags]"
}
if len(cmd.order) > 0 {
usage += " <command>"
}
return usage
}
// hasAnyVisibleFlags reports whether a local command help screen has visible flags.
func hasAnyVisibleFlags(fs *FlagSet) bool {
for _, fl := range fs.impl.OrderedStaticFlags() {
if !fl.Hidden {
return true
}
}
for _, group := range fs.impl.DynamicGroups() {
if group.IsHidden() {
continue
}
for _, fl := range group.DynamicFlags() {
if !fl.Hidden {
return true
}
}
}
return false
}
// longestCommandName returns the widest child command name for aligned help output.
func longestCommandName(commands []*Command) int {
width := 0
for _, cmd := range commands {
if len(cmd.name) > width {
width = len(cmd.name)
}
}
return width
}