-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
131 lines (103 loc) · 2.74 KB
/
command.go
File metadata and controls
131 lines (103 loc) · 2.74 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
package cli
import (
"context"
"fmt"
"os"
"strings"
"github.com/bobg/errors"
"github.com/broothie/option"
)
type Handler func(ctx context.Context) error
type Command struct {
name string
description string
version string
aliases []string
parent *Command
subCommands []*Command
flags []*Flag
arguments []*Argument
handler Handler
}
// NewCommand creates a new command.
func NewCommand(name, description string, options ...option.Option[*Command]) (*Command, error) {
baseCommand := &Command{
name: name,
description: description,
}
command, err := option.Apply(baseCommand, options...)
if err != nil {
return nil, errors.Wrapf(err, "building command %q", name)
}
if err := command.validateConfig(); err != nil {
return nil, errors.Wrapf(err, "invalid command %q", name)
}
return command, nil
}
// Run creates and runs a command using os.Args as the arguments and context.Background as the context.
func Run(name, description string, options ...option.Option[*Command]) {
command, err := NewCommand(name, description, options...)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := command.Run(context.Background(), os.Args[1:]); err != nil {
ExitWithError(err)
}
}
// Run runs the command.
func (c *Command) Run(ctx context.Context, rawArgs []string) error {
if commandProcessed, err := c.newParser(rawArgs).parse(ctx); err != nil {
return err
} else if commandProcessed {
return nil
}
return c.runHandler(ctx)
}
func (c *Command) runHandler(ctx context.Context) error {
if c.isHelpFlagAsserted() {
return c.renderHelp(os.Stdout)
} else if c.isVersionFlagAsserted() {
fmt.Println(c.findVersion())
return nil
}
if err := c.validateInput(); err != nil {
return err
}
if c.handler == nil {
return c.renderHelp(os.Stdout)
}
return c.handler(c.onContext(ctx))
}
func (c *Command) isHelpFlagAsserted() bool {
flag, found := c.findFlagUpToRoot(func(flag *Flag) bool { return flag.isHelp })
return found && flag.isBool() && flag.value != nil && flag.value.(bool)
}
func (c *Command) isVersionFlagAsserted() bool {
flag, found := c.findFlagUpToRoot(func(flag *Flag) bool { return flag.isVersion })
return found && flag.isBool() && flag.value != nil && flag.value.(bool)
}
func (c *Command) root() *Command {
if c.isRoot() {
return c
}
return c.parent.root()
}
func (c *Command) isRoot() bool {
return c.parent == nil
}
func (c *Command) hasParent() bool {
return !c.isRoot()
}
func (c *Command) qualifiedName() string {
if c.hasParent() {
return strings.Join([]string{c.parent.qualifiedName(), c.name}, " ")
}
return c.name
}
func (c *Command) findVersion() string {
if c.version == "" && c.hasParent() {
return c.parent.findVersion()
}
return c.version
}