cmd: Fix panic if cmd ends on dash or underscore

This commit is contained in:
Albert Nigmatzianov 2017-05-05 10:30:41 +02:00
parent efbe9b05d8
commit 7bb5276f5f
2 changed files with 8 additions and 0 deletions

View file

@ -77,6 +77,12 @@ func validateCmdName(source string) string {
output = source[:i] 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, // If next character is dash or underscore,
// just skip the current character. // just skip the current character.
if source[i+1] == '-' || source[i+1] == '_' { if source[i+1] == '-' || source[i+1] == '_' {

View file

@ -87,6 +87,8 @@ func TestValidateCmdName(t *testing.T) {
{"cmd------Name", "cmdName"}, {"cmd------Name", "cmdName"},
{"cmd______name", "cmdName"}, {"cmd______name", "cmdName"},
{"cmd------name", "cmdName"}, {"cmd------name", "cmdName"},
{"cmdName-----", "cmdName"},
{"cmdname-", "cmdname"},
} }
for _, testCase := range testCases { for _, testCase := range testCases {