mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
cmd: Fix panic if cmd ends on dash or underscore
This commit is contained in:
parent
efbe9b05d8
commit
7bb5276f5f
2 changed files with 8 additions and 0 deletions
|
@ -77,6 +77,12 @@ func validateCmdName(source string) string {
|
|||
output = source[:i]
|
||||
}
|
||||
|
||||
// If it's last rune and it's dash or underscore,
|
||||
// don't add it output and break the loop.
|
||||
if i == l-1 {
|
||||
break
|
||||
}
|
||||
|
||||
// If next character is dash or underscore,
|
||||
// just skip the current character.
|
||||
if source[i+1] == '-' || source[i+1] == '_' {
|
||||
|
|
|
@ -87,6 +87,8 @@ func TestValidateCmdName(t *testing.T) {
|
|||
{"cmd------Name", "cmdName"},
|
||||
{"cmd______name", "cmdName"},
|
||||
{"cmd------name", "cmdName"},
|
||||
{"cmdName-----", "cmdName"},
|
||||
{"cmdname-", "cmdname"},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
|
|
Loading…
Reference in a new issue