Skip to content

Commit a0148c8

Browse files
committed
Rename Cmd to Command
Creates better parity with cobra, plus my original rationale for using Cmd is a bit flawed as the semantics of a serpent command are nothing alike a go std lib Cmd.
1 parent e20c1db commit a0148c8

File tree

5 files changed

+48
-46
lines changed

5 files changed

+48
-46
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
func main() {
3232
var upper bool
33-
cmd := serpent.Cmd{
33+
cmd := serpent.Command{
3434
Use: "echo <text>",
3535
Short: "Prints the given text to the console.",
3636
Options: serpent.OptionSet{
@@ -86,7 +86,7 @@ type Option struct {
8686
}
8787
```
8888

89-
And is used by each [Cmd](https://pkg.go.dev/github.com/coder/serpent#Cmd) when
89+
And is used by each [Command](https://pkg.go.dev/github.com/coder/serpent#Command) when
9090
passed as an array to the `Options` field.
9191

9292
## More coming...

cmd.go renamed to command.go

+18-16
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ import (
2121
"gopkg.in/yaml.v3"
2222
)
2323

24-
// Cmd describes an executable command.
25-
type Cmd struct {
24+
// Command describes an executable command.
25+
type Command struct {
2626
// Parent is the direct parent of the command.
2727
//
2828
// It is set automatically when an invokation runs.
29-
Parent *Cmd
29+
Parent *Command
30+
3031
// Children is a list of direct descendants.
31-
Children []*Cmd
32+
Children []*Command
33+
3234
// Use is provided in form "command [flags] [args...]".
3335
Use string
3436

@@ -61,15 +63,15 @@ type Cmd struct {
6163

6264
// AddSubcommands adds the given subcommands, setting their
6365
// Parent field automatically.
64-
func (c *Cmd) AddSubcommands(cmds ...*Cmd) {
66+
func (c *Command) AddSubcommands(cmds ...*Command) {
6567
for _, cmd := range cmds {
6668
cmd.Parent = c
6769
c.Children = append(c.Children, cmd)
6870
}
6971
}
7072

7173
// Walk calls fn for the command and all its children.
72-
func (c *Cmd) Walk(fn func(*Cmd)) {
74+
func (c *Command) Walk(fn func(*Command)) {
7375
fn(c)
7476
for _, child := range c.Children {
7577
child.Parent = c
@@ -87,7 +89,7 @@ func ascendingSortFn[T constraints.Ordered](a, b T) int {
8789
}
8890

8991
// init performs initialization and linting on the command and all its children.
90-
func (c *Cmd) init() error {
92+
func (c *Command) init() error {
9193
if c.Use == "" {
9294
c.Use = "unnamed"
9395
}
@@ -121,7 +123,7 @@ func (c *Cmd) init() error {
121123
slices.SortFunc(c.Options, func(a, b Option) int {
122124
return ascendingSortFn(a.Name, b.Name)
123125
})
124-
slices.SortFunc(c.Children, func(a, b *Cmd) int {
126+
slices.SortFunc(c.Children, func(a, b *Command) int {
125127
return ascendingSortFn(a.Name(), b.Name())
126128
})
127129
for _, child := range c.Children {
@@ -135,13 +137,13 @@ func (c *Cmd) init() error {
135137
}
136138

137139
// Name returns the first word in the Use string.
138-
func (c *Cmd) Name() string {
140+
func (c *Command) Name() string {
139141
return strings.Split(c.Use, " ")[0]
140142
}
141143

142144
// FullName returns the full invocation name of the command,
143145
// as seen on the command line.
144-
func (c *Cmd) FullName() string {
146+
func (c *Command) FullName() string {
145147
var names []string
146148
if c.Parent != nil {
147149
names = append(names, c.Parent.FullName())
@@ -152,7 +154,7 @@ func (c *Cmd) FullName() string {
152154

153155
// FullName returns usage of the command, preceded
154156
// by the usage of its parents.
155-
func (c *Cmd) FullUsage() string {
157+
func (c *Command) FullUsage() string {
156158
var uses []string
157159
if c.Parent != nil {
158160
uses = append(uses, c.Parent.FullName())
@@ -162,7 +164,7 @@ func (c *Cmd) FullUsage() string {
162164
}
163165

164166
// FullOptions returns the options of the command and its parents.
165-
func (c *Cmd) FullOptions() OptionSet {
167+
func (c *Command) FullOptions() OptionSet {
166168
var opts OptionSet
167169
if c.Parent != nil {
168170
opts = append(opts, c.Parent.FullOptions()...)
@@ -175,7 +177,7 @@ func (c *Cmd) FullOptions() OptionSet {
175177
// stdio discarded.
176178
//
177179
// The returned invocation is not live until Run() is called.
178-
func (c *Cmd) Invoke(args ...string) *Invocation {
180+
func (c *Command) Invoke(args ...string) *Invocation {
179181
return &Invocation{
180182
Command: c,
181183
Args: args,
@@ -189,7 +191,7 @@ func (c *Cmd) Invoke(args ...string) *Invocation {
189191
// Invocation represents an instance of a command being executed.
190192
type Invocation struct {
191193
ctx context.Context
192-
Command *Cmd
194+
Command *Command
193195
parsedFlags *pflag.FlagSet
194196
Args []string
195197
// Environ is a list of environment variables. Use EnvsWithPrefix to parse
@@ -291,7 +293,7 @@ func (inv *Invocation) run(state *runState) error {
291293

292294
// Now the fun part, argument parsing!
293295

294-
children := make(map[string]*Cmd)
296+
children := make(map[string]*Command)
295297
for _, child := range inv.Command.Children {
296298
child.Parent = inv.Command
297299
for _, name := range append(child.Aliases, child.Name()) {
@@ -447,7 +449,7 @@ func (inv *Invocation) run(state *runState) error {
447449
}
448450

449451
type RunCommandError struct {
450-
Cmd *Cmd
452+
Cmd *Command
451453
Err error
452454
}
453455

cmd_test.go renamed to command_test.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ func fakeIO(i *serpent.Invocation) *ioBufs {
3333
func TestCommand(t *testing.T) {
3434
t.Parallel()
3535

36-
cmd := func() *serpent.Cmd {
36+
cmd := func() *serpent.Command {
3737
var (
3838
verbose bool
3939
lower bool
4040
prefix string
4141
reqBool bool
4242
reqStr string
4343
)
44-
return &serpent.Cmd{
44+
return &serpent.Command{
4545
Use: "root [subcommand]",
4646
Options: serpent.OptionSet{
4747
serpent.Option{
@@ -55,7 +55,7 @@ func TestCommand(t *testing.T) {
5555
Value: serpent.StringOf(&prefix),
5656
},
5757
},
58-
Children: []*serpent.Cmd{
58+
Children: []*serpent.Command{
5959
{
6060
Use: "required-flag --req-bool=true --req-string=foo",
6161
Short: "Example with required flags",
@@ -320,12 +320,12 @@ func TestCommand(t *testing.T) {
320320

321321
func TestCommand_DeepNest(t *testing.T) {
322322
t.Parallel()
323-
cmd := &serpent.Cmd{
323+
cmd := &serpent.Command{
324324
Use: "1",
325-
Children: []*serpent.Cmd{
325+
Children: []*serpent.Command{
326326
{
327327
Use: "2",
328-
Children: []*serpent.Cmd{
328+
Children: []*serpent.Command{
329329
{
330330
Use: "3",
331331
Handler: func(i *serpent.Invocation) error {
@@ -348,7 +348,7 @@ func TestCommand_FlagOverride(t *testing.T) {
348348
t.Parallel()
349349
var flag string
350350

351-
cmd := &serpent.Cmd{
351+
cmd := &serpent.Command{
352352
Use: "1",
353353
Options: serpent.OptionSet{
354354
{
@@ -357,7 +357,7 @@ func TestCommand_FlagOverride(t *testing.T) {
357357
Value: serpent.DiscardValue,
358358
},
359359
},
360-
Children: []*serpent.Cmd{
360+
Children: []*serpent.Command{
361361
{
362362
Use: "2",
363363
Options: serpent.OptionSet{
@@ -392,7 +392,7 @@ func TestCommand_MiddlewareOrder(t *testing.T) {
392392
}
393393
}
394394

395-
cmd := &serpent.Cmd{
395+
cmd := &serpent.Command{
396396
Use: "toupper [word]",
397397
Short: "Converts a word to upper case",
398398
Middleware: serpent.Chain(
@@ -416,8 +416,8 @@ func TestCommand_MiddlewareOrder(t *testing.T) {
416416
func TestCommand_RawArgs(t *testing.T) {
417417
t.Parallel()
418418

419-
cmd := func() *serpent.Cmd {
420-
return &serpent.Cmd{
419+
cmd := func() *serpent.Command {
420+
return &serpent.Command{
421421
Use: "root",
422422
Options: serpent.OptionSet{
423423
{
@@ -426,7 +426,7 @@ func TestCommand_RawArgs(t *testing.T) {
426426
Value: serpent.StringOf(new(string)),
427427
},
428428
},
429-
Children: []*serpent.Cmd{
429+
Children: []*serpent.Command{
430430
{
431431
Use: "sushi <args...>",
432432
Short: "Throws back raw output",
@@ -480,7 +480,7 @@ func TestCommand_RawArgs(t *testing.T) {
480480

481481
func TestCommand_RootRaw(t *testing.T) {
482482
t.Parallel()
483-
cmd := &serpent.Cmd{
483+
cmd := &serpent.Command{
484484
RawArgs: true,
485485
Handler: func(i *serpent.Invocation) error {
486486
_, _ = i.Stdout.Write([]byte(strings.Join(i.Args, " ")))
@@ -498,7 +498,7 @@ func TestCommand_RootRaw(t *testing.T) {
498498

499499
func TestCommand_HyphenHyphen(t *testing.T) {
500500
t.Parallel()
501-
cmd := &serpent.Cmd{
501+
cmd := &serpent.Command{
502502
Handler: (func(i *serpent.Invocation) error {
503503
_, _ = i.Stdout.Write([]byte(strings.Join(i.Args, " ")))
504504
return nil
@@ -518,7 +518,7 @@ func TestCommand_ContextCancels(t *testing.T) {
518518

519519
var gotCtx context.Context
520520

521-
cmd := &serpent.Cmd{
521+
cmd := &serpent.Command{
522522
Handler: (func(i *serpent.Invocation) error {
523523
gotCtx = i.Context()
524524
if err := gotCtx.Err(); err != nil {
@@ -537,8 +537,8 @@ func TestCommand_ContextCancels(t *testing.T) {
537537
func TestCommand_Help(t *testing.T) {
538538
t.Parallel()
539539

540-
cmd := func() *serpent.Cmd {
541-
return &serpent.Cmd{
540+
cmd := func() *serpent.Command {
541+
return &serpent.Command{
542542
Use: "root",
543543
HelpHandler: (func(i *serpent.Invocation) error {
544544
_, _ = i.Stdout.Write([]byte("abdracadabra"))
@@ -585,9 +585,9 @@ func TestCommand_Help(t *testing.T) {
585585
func TestCommand_SliceFlags(t *testing.T) {
586586
t.Parallel()
587587

588-
cmd := func(want ...string) *serpent.Cmd {
588+
cmd := func(want ...string) *serpent.Command {
589589
var got []string
590-
return &serpent.Cmd{
590+
return &serpent.Command{
591591
Use: "root",
592592
Options: serpent.OptionSet{
593593
{
@@ -614,9 +614,9 @@ func TestCommand_SliceFlags(t *testing.T) {
614614
func TestCommand_EmptySlice(t *testing.T) {
615615
t.Parallel()
616616

617-
cmd := func(want ...string) *serpent.Cmd {
617+
cmd := func(want ...string) *serpent.Command {
618618
var got []string
619-
return &serpent.Cmd{
619+
return &serpent.Command{
620620
Use: "root",
621621
Options: serpent.OptionSet{
622622
{
@@ -667,7 +667,7 @@ func TestCommand_DefaultsOverride(t *testing.T) {
667667
got string
668668
config serpent.YAMLConfigPath
669669
)
670-
cmd := &serpent.Cmd{
670+
cmd := &serpent.Command{
671671
Options: serpent.OptionSet{
672672
{
673673
Name: "url",

example/echo/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func main() {
1111
var upper bool
12-
cmd := serpent.Cmd{
12+
cmd := serpent.Command{
1313
Use: "echo <text>",
1414
Short: "Prints the given text to the console.",
1515
Options: serpent.OptionSet{

help.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ var defaultHelpTemplate = func() *template.Template {
123123
}
124124
return sb.String()
125125
},
126-
"rootCommandName": func(cmd *Cmd) string {
126+
"rootCommandName": func(cmd *Command) string {
127127
return strings.Split(cmd.FullName(), " ")[0]
128128
},
129-
"formatSubcommand": func(cmd *Cmd) string {
129+
"formatSubcommand": func(cmd *Command) string {
130130
// Minimize padding by finding the longest neighboring name.
131131
maxNameLength := len(cmd.Name())
132132
if parent := cmd.Parent; parent != nil {
@@ -205,12 +205,12 @@ var defaultHelpTemplate = func() *template.Template {
205205
s = wrapTTY(s)
206206
return s
207207
},
208-
"visibleChildren": func(cmd *Cmd) []*Cmd {
209-
return filterSlice(cmd.Children, func(c *Cmd) bool {
208+
"visibleChildren": func(cmd *Command) []*Command {
209+
return filterSlice(cmd.Children, func(c *Command) bool {
210210
return !c.Hidden
211211
})
212212
},
213-
"optionGroups": func(cmd *Cmd) []optionGroup {
213+
"optionGroups": func(cmd *Command) []optionGroup {
214214
groups := []optionGroup{{
215215
// Default group.
216216
Name: "",

0 commit comments

Comments
 (0)