Skip to content

Commit 163b6b3

Browse files
committed
Update name regex to allow underscore and dash
1 parent 22b3f1e commit 163b6b3

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

parse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ func Parse(root *Command, args []string) error {
206206
return nil
207207
}
208208

209-
var validNameRegex = regexp.MustCompile(`^[a-zA-Z]+$`)
209+
var validNameRegex = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_-]*$`)
210210

211211
func validateName(root *Command) error {
212212
if !validNameRegex.MatchString(root.Name) {
213-
return fmt.Errorf("must contain only letters, no spaces or special characters")
213+
return fmt.Errorf("name must start with a letter and contain only letters, numbers, dashes (-) or underscores (_)")
214214
}
215215
return nil
216216
}

parse_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,19 @@ func TestParse(t *testing.T) {
382382
}
383383
err := Parse(cmd, nil)
384384
require.Error(t, err)
385-
require.ErrorContains(t, err, `failed to parse: command ["root", "sub command"]: must contain only letters, no spaces or special characters`)
385+
require.ErrorContains(t, err, `failed to parse: command ["root", "sub command"]: name must start with a letter and contain only letters, numbers, dashes (-) or underscores (_)`)
386+
})
387+
t.Run("dash in command name", func(t *testing.T) {
388+
t.Parallel()
389+
cmd := &Command{
390+
Name: "root",
391+
Exec: func(ctx context.Context, s *State) error { return nil },
392+
SubCommands: []*Command{
393+
{Name: "sub-command"},
394+
},
395+
}
396+
err := Parse(cmd, nil)
397+
require.NoError(t, err)
386398
})
387399
}
388400

0 commit comments

Comments
 (0)