-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
62 lines (56 loc) · 1.36 KB
/
command.go
File metadata and controls
62 lines (56 loc) · 1.36 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
package cli
// Root returns the Root command.
func (cmd *Command) Root() *Command {
if cmd.Parent == nil {
return cmd
}
return cmd.Parent.Root()
}
// OptMap returns a mapping of options by their
// names and aliases for this command.
func (cmd *Command) OptMap() map[string]*Opt {
res := map[string]*Opt{}
cmd.PutMap(res)
return res
}
func (cmd *Command) PutMap(m map[string]*Opt) map[string]*Opt {
for i := range cmd.Opts {
o := cmd.Opts[i]
m[o.Name] = o
for _, al := range o.Aliases {
m[al] = o
}
}
return m
}
// Path returns the path in the command tree to this command.
func (cmd *Command) Path() []*Command {
if cmd.Parent == nil {
return []*Command{cmd}
}
return append(cmd.Parent.Path(), cmd)
}
// PutOptsAll places the options for all commands in the [Command.Path]
// of cmd, filtering out invalidated options along the way.
func (cmd *Command) PutOptsAll(dst map[string]*Opt) map[string]*Opt {
res := map[string]*Opt{}
for _, o := range cmd.Path() {
res = o.PutMap(res)
for k := range o.InvalidOpts {
io := res[k]
if io == nil {
continue
}
for _, al := range io.Aliases {
delete(res, al)
}
delete(res, k)
}
}
return res
}
// AllOpts returns all options available when cmd
// is [Command.Run], keyed by name and alias.
func (cmd *Command) AllOpts() map[string]*Opt {
return cmd.PutOptsAll(map[string]*Opt{})
}