mirror of https://github.com/spf13/cobra.git
cmd: Fix panic if cmd ends on dash or underscore
This commit is contained in:
parent
efbe9b05d8
commit
7bb5276f5f
|
@ -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 New Issue