-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
187 lines (148 loc) · 4.93 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
// SPDX-FileCopyrightText: 2025 The Cipher Host Team <[email protected]>
//
// SPDX-License-Identifier: MIT
package cmdkit
import (
"errors"
"flag"
"fmt"
"io"
"go.cipher.host/cmdkit/internal/xflag"
)
// ErrCommandRunMissing is returned when a command is created without the RunE
// field.
var ErrCommandRunMissing Error = "command has no run function"
// Command represents a single subcommand in a command-line application. It
// encapsulates all the behavior and flags specific to one function of the
// application, following the Unix principle of small, focused tools.
//
// Commands are designed to be self-contained units that can be composed into
// larger applications. Each command manages its own flags and handles its own
// argument validation and processing.
type Command struct {
// Flags holds command-specific flags that only apply to this command. These
// are parsed after global flags but before command arguments.
Flags *flag.FlagSet
// RunE executes the command's logic. It receives the remaining arguments
// after flag parsing.
RunE func(args []string) error
// Before runs before command execution, allowing for command-specific setup
// like validating flags or preparing resources.
Before func() error
// After runs after command execution for cleanup. Executes even if the
// command fails.
After func() error
// app is the parent application, providing access to global state and
// configuration.
app *App
// Name is the command name as used in the command line.
Name string
// Description explains the command's purpose. Shown in both parent app help
// and command-specific help.
Description string
// Usage describes the command's argument pattern. E.g., "[flags] <input>
// <output>".
Usage string
// Examples shows example invocations of this specific command.
Examples []string
// shorthands maps short flag names, e.g. "-h", to their long versions, e.g.
// "--help". This enables Unix-style short flags while maintaining clean
// flag names.
shorthands []xflag.Shorthand
// helpFlag indicates whether to show helpFlag text.
helpFlag bool
}
// NewCommand creates a new Command instance with the provided metadata.
//
// For the best user experience, the usage string should follow Unix
// conventions:
// - Optional elements in square brackets: [--flag].
// - Required elements in angle brackets: <file>.
// - Variable numbers of arguments with ellipsis: [file...].
func NewCommand(name, description, usage string) *Command {
cmd := &Command{
Name: name,
Description: description,
Usage: usage,
Flags: flag.NewFlagSet(name, flag.ContinueOnError),
}
cmd.Flags.BoolVar(&cmd.helpFlag, "help", false, "show help")
cmd.Flags.BoolVar(&cmd.helpFlag, "h", false, "show help")
cmd.AddShorthand("help", "h")
cmd.Flags.Usage = cmd.ShowHelp
return cmd
}
// AddShorthand creates a short form alias for a command-specific flag. This
// works the same way as [App.AddShorthand] but scoped to the command's flags.
//
// [app.AddShorthand]: https://pkg.go.dev/go.cipher.host/cmdkit#App.AddShorthand
func (c *Command) AddShorthand(name, shorthand string) {
if f := c.Flags.Lookup(name); f != nil {
c.shorthands = append(c.shorthands, xflag.Shorthand{
Name: name,
Shorthand: shorthand,
Value: f.Value,
})
}
}
// Run executes the command with the provided arguments. It follows
// a similar flow to [App.Run].
//
// [App.Run]: https://pkg.go.dev/go.cipher.host/cmdkit#App.Run
func (c *Command) Run(args []string) error {
c.Flags.SetOutput(c.app.ErrWriter)
if err := c.Flags.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return fmt.Errorf("%w", err)
}
if c.helpFlag {
c.ShowHelp()
return nil
}
if c.Before != nil {
if err := c.Before(); err != nil {
return fmt.Errorf("%w", err)
}
}
defer func() {
if c.After != nil {
_ = c.After() //nolint:errcheck // not much we can do here
}
}()
if c.RunE == nil {
return fmt.Errorf("%q %w", c.Name, ErrCommandRunMissing)
}
return c.RunE(c.Flags.Args())
}
// OutWriter returns the application's output writer.
func (c *Command) OutWriter() io.Writer {
return c.app.OutWriter
}
// ErrWriter returns the application's error writer.
func (c *Command) ErrWriter() io.Writer {
return c.app.ErrWriter
}
// ShowHelp displays command-specific help text.
func (c *Command) ShowHelp() {
fmt.Fprintf(c.app.ErrWriter, "Usage: %s %s", c.app.Name, c.Name)
if c.Usage != "" {
fmt.Fprintf(c.app.ErrWriter, " %s", c.Usage)
}
fmt.Fprintln(c.app.ErrWriter)
fmt.Fprintln(c.app.ErrWriter)
if c.Description != "" {
fmt.Fprintf(c.app.ErrWriter, "%s\n\n", c.Description)
}
fmt.Fprintf(c.app.ErrWriter, "Flags:\n")
c.Flags.SetOutput(c.app.ErrWriter)
xflag.PrintFlags(c.app.ErrWriter, c.Flags, c.shorthands)
fmt.Fprintln(c.app.ErrWriter)
if len(c.Examples) > 0 {
fmt.Fprintf(c.app.ErrWriter, "Examples:\n")
for _, example := range c.Examples {
fmt.Fprintf(c.app.ErrWriter, " $ %s\n", example)
}
}
}