Skip to content

Commit

Permalink
up: cflag - add a new util func FilterNames()
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 11, 2022
1 parent 233578f commit baca9d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
20 changes: 12 additions & 8 deletions cflag/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,22 @@ func AddPrefixes2(name string, shorts []string, nameAtEnd bool) string {
}

// SplitShortcut string to []string
func SplitShortcut(shortcut string) (shorts []string) {
for _, sub := range strings.Split(shortcut, ",") {
sub = strings.TrimSpace(sub)
if sub != "" {
sub = strings.Trim(sub, "- ")
func SplitShortcut(shortcut string) []string {
return FilterNames(strings.Split(shortcut, ","))
}

// FilterNames for option names
func FilterNames(names []string) []string {
filtered := make([]string, 0, len(names))
for _, sub := range names {
if sub = strings.TrimSpace(sub); sub != "" {
sub = strings.Trim(sub, "-+= ")
if sub != "" {
shorts = append(shorts, sub)
filtered = append(filtered, sub)
}
}
}

return
return filtered
}

// IsFlagHelpErr check
Expand Down
1 change: 1 addition & 0 deletions cflag/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestIsFlagHelpErr(t *testing.T) {

func TestSplitShortcut(t *testing.T) {
assert.Eq(t, []string{"a", "b"}, cflag.SplitShortcut("a,-b"))
assert.Eq(t, []string{"a", "b"}, cflag.SplitShortcut("a, ,-b"))
assert.Eq(t, []string{"ab", "cd"}, cflag.SplitShortcut("-- ab,,-cd"))
}

Expand Down

0 comments on commit baca9d5

Please sign in to comment.