diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index 9e30671..20c6538 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -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] == '_' { diff --git a/cobra/cmd/add_test.go b/cobra/cmd/add_test.go index 6c1ec0f..dacbe83 100644 --- a/cobra/cmd/add_test.go +++ b/cobra/cmd/add_test.go @@ -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 {