Skip to content

Commit 184f5e7

Browse files
committed
docs: comment and refactor NewCmd
1 parent 350c74b commit 184f5e7

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

builder.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"runtime/debug"
55
"slices"
66
"strings"
7+
"unicode"
78
)
89

910
var DefaultHelpInput = NewBoolOpt("help").
@@ -109,25 +110,25 @@ func (c *CommandInfo) prepareAndValidate() {
109110
}
110111
}
111112

113+
// NewCmd returns a new CommandInfo with the Name field set to name and Path field set to
114+
// a single element slice of name. This function will panic if name is empty or contains
115+
// whitespace.
112116
func NewCmd(name string) CommandInfo {
113117
// assert command name isn't empty and doesn't contain any whitespace
114118
if name == "" {
115119
panic(errEmptyCmdName)
116120
}
117-
for i := range name {
118-
switch name[i] {
119-
case ' ', '\t', '\n', '\r':
120-
panic("invalid command name '" + name + "': cannot contain whitespace")
121-
}
121+
if strings.ContainsFunc(name, unicode.IsSpace) {
122+
panic("invalid command name '" + name + "': cannot contain whitespace")
122123
}
123-
124124
return CommandInfo{
125125
Name: name,
126126
Path: []string{name},
127127
Opts: make([]InputInfo, 0, 5),
128128
}
129129
}
130130

131+
// Help sets the HelpBlurb field of this command to blurb.
131132
func (c CommandInfo) Help(blurb string) CommandInfo {
132133
c.HelpBlurb = blurb
133134
return c

0 commit comments

Comments
 (0)