Skip to content

Commit 8356d2a

Browse files
committed
refactor: use strings instead of custom types for builder panics
1 parent 72226b8 commit 8356d2a

File tree

2 files changed

+22
-62
lines changed

2 files changed

+22
-62
lines changed

builder.go

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var DefaultHelpInput = NewBoolOpt("help").
99

1010
var (
1111
errMixingPosArgsAndSubcmds = "commands cannot have both positional args and subcommands"
12-
errEmptyCmdName = invalidCmdNameError{}
12+
errEmptyCmdName = "empty command name"
1313
errEmptyInputID = "inputs must have non-empty, unique ids"
1414
errEmptyOptNames = "options must have either a short or long name"
1515
errOptAsPosArg = "adding an option as a positional argument"
@@ -27,27 +27,18 @@ func (c *CommandInfo) validate() {
2727
for z := i + 1; z < len(c.opts); z++ {
2828
// assert there are no duplicate input ids
2929
if c.opts[i].id == c.opts[z].id {
30-
panic(illegalDupError{
31-
cmdPath: strings.Join(c.path, " "),
32-
what: "ids",
33-
dups: c.opts[i].id,
34-
})
30+
panic("command '" + strings.Join(c.path, " ") +
31+
"' contains duplicate option ids '" + c.opts[i].id + "'")
3532
}
3633

3734
// assert there are no duplicate long or short option names
3835
if c.opts[i].nameShort != "" && c.opts[i].nameShort == c.opts[z].nameShort {
39-
panic(illegalDupError{
40-
cmdPath: strings.Join(c.path, " "),
41-
what: "option short names",
42-
dups: c.opts[i].nameShort,
43-
})
36+
panic("command '" + strings.Join(c.path, " ") +
37+
"' contains duplicate option short name '" + c.opts[i].nameShort + "'")
4438
}
4539
if c.opts[i].nameLong != "" && c.opts[i].nameLong == c.opts[z].nameLong {
46-
panic(illegalDupError{
47-
cmdPath: strings.Join(c.path, " "),
48-
what: "option long names",
49-
dups: c.opts[i].nameLong,
50-
})
40+
panic("command '" + strings.Join(c.path, " ") +
41+
"' contains duplicate option long name '" + c.opts[i].nameLong + "'")
5142
}
5243
}
5344
}
@@ -56,11 +47,8 @@ func (c *CommandInfo) validate() {
5647
for i := 0; i < len(c.args)-1; i++ {
5748
for z := i + 1; z < len(c.args); z++ {
5849
if c.args[i].id == c.args[z].id {
59-
panic(illegalDupError{
60-
cmdPath: strings.Join(c.path, " "),
61-
what: "ids",
62-
dups: c.args[i].id,
63-
})
50+
panic("command '" + strings.Join(c.path, " ") +
51+
"' contains duplicate argument ids '" + c.args[i].id + "'")
6452
}
6553
}
6654
}
@@ -69,11 +57,8 @@ func (c *CommandInfo) validate() {
6957
for i := 0; i < len(c.subcmds)-1; i++ {
7058
for z := i + 1; z < len(c.subcmds); z++ {
7159
if c.subcmds[i].name == c.subcmds[z].name {
72-
panic(illegalDupError{
73-
cmdPath: strings.Join(c.path, " "),
74-
what: "subcommand names",
75-
dups: c.subcmds[i].name,
76-
})
60+
panic("command '" + strings.Join(c.path, " ") +
61+
"' contains duplicate subcommand name '" + c.subcmds[i].name + "'")
7762
}
7863
}
7964
}
@@ -83,28 +68,6 @@ func (c *CommandInfo) validate() {
8368
}
8469
}
8570

86-
type illegalDupError struct {
87-
cmdPath string
88-
what string // input ids, option short names, option long names, subcmd names
89-
dups string
90-
}
91-
92-
func (e illegalDupError) String() string {
93-
return "command '" + e.cmdPath + "' contains options with duplicate " + e.what + " '" + e.dups + "'"
94-
}
95-
96-
type invalidCmdNameError struct {
97-
name string
98-
reason string
99-
}
100-
101-
func (e invalidCmdNameError) String() string {
102-
if e.name == "" {
103-
return "empty command name"
104-
}
105-
return "invalid command name '" + e.name + "': " + e.reason
106-
}
107-
10871
func NewCmd(name string) CommandInfo {
10972
// assert command name isn't empty and doesn't contain any whitespace
11073
if name == "" {
@@ -113,10 +76,7 @@ func NewCmd(name string) CommandInfo {
11376
for i := range name {
11477
switch name[i] {
11578
case ' ', '\t', '\n', '\r':
116-
panic(invalidCmdNameError{
117-
name: name,
118-
reason: "cannot contain whitespace",
119-
})
79+
panic("invalid command name '" + name + "': cannot contain whitespace")
12080
}
12181
}
12282

builder_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ func TestBuilder(t *testing.T) {
5757
func() { NewCmd("ab").Subcmd(NewCmd("c\td")) },
5858
},
5959
expPanicVals: []any{
60-
invalidCmdNameError{name: " ", reason: "cannot contain whitespace"},
61-
invalidCmdNameError{name: "a b", reason: "cannot contain whitespace"},
62-
invalidCmdNameError{name: "c\td", reason: "cannot contain whitespace"},
60+
"invalid command name ' ': cannot contain whitespace",
61+
"invalid command name 'a b': cannot contain whitespace",
62+
"invalid command name 'c\td': cannot contain whitespace",
6363
},
6464
},
6565
{
@@ -104,8 +104,8 @@ func TestBuilder(t *testing.T) {
104104
},
105105
},
106106
expPanicVals: []any{
107-
illegalDupError{cmdPath: "root one", what: "ids", dups: "o1"},
108-
illegalDupError{cmdPath: "root", what: "ids", dups: "a1"},
107+
"command 'root one' contains duplicate option ids 'o1'",
108+
"command 'root' contains duplicate argument ids 'a1'",
109109
},
110110
},
111111
{
@@ -132,8 +132,8 @@ func TestBuilder(t *testing.T) {
132132
},
133133
expPanicVals: []any{
134134
nil,
135-
illegalDupError{cmdPath: "root", what: "option short names", dups: "a"},
136-
illegalDupError{cmdPath: "root", what: "option short names", dups: "b"},
135+
"command 'root' contains duplicate option short name 'a'",
136+
"command 'root' contains duplicate option short name 'b'",
137137
},
138138
},
139139
{
@@ -148,7 +148,7 @@ func TestBuilder(t *testing.T) {
148148
},
149149
},
150150
expPanicVals: []any{
151-
illegalDupError{cmdPath: "root", what: "option long names", dups: "aaa"},
151+
"command 'root' contains duplicate option long name 'aaa'",
152152
},
153153
},
154154
{
@@ -202,8 +202,8 @@ func TestBuilder(t *testing.T) {
202202
},
203203
},
204204
expPanicVals: []any{
205-
illegalDupError{cmdPath: "root", what: "subcommand names", dups: "bb"},
206-
illegalDupError{cmdPath: "root subcmd", what: "subcommand names", dups: "aa"},
205+
"command 'root' contains duplicate subcommand name 'bb'",
206+
"command 'root subcmd' contains duplicate subcommand name 'aa'",
207207
},
208208
},
209209
} {

0 commit comments

Comments
 (0)