Skip to content

Commit

Permalink
Fix various bugs around word splitting (#51)
Browse files Browse the repository at this point in the history
* Fix various bugs around word splitting

Impacts:
* option names splitting
* command alias splitting
* Env var name splitting

Bug:
A naive `strings.Split(val, " ")` was performed to split a value into words.
If a values contains 2+ spaces, an empty value is returned in the word list.
  • Loading branch information
jawher authored Aug 2, 2017
1 parent a608864 commit 82aefbe
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ the last argument, init, is a function that will be called by mow.cli to further
(sub) command, e.g. to add options, arguments and the code to execute
*/
func (c *Cmd) Command(name, desc string, init CmdInitializer) {
aliases := strings.Split(name, " ")
aliases := strings.Fields(name)
c.commands = append(c.commands, &Cmd{
ErrorHandling: c.ErrorHandling,
name: aliases[0],
Expand Down Expand Up @@ -389,7 +389,7 @@ func (c *Cmd) formatDescription(desc, envVar string) string {
if len(envVar) > 0 {
b.WriteString(" (")
sep := ""
for _, envVal := range strings.Split(envVar, " ") {
for _, envVal := range strings.Fields(envVar) {
b.WriteString(fmt.Sprintf("%s$%s", sep, envVal))
sep = " "
}
Expand Down
8 changes: 4 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,15 @@ func (o *opt) String() string {
}

func mkOptStrs(optName string) []string {
namesSl := strings.Split(optName, " ")
for i, name := range namesSl {
res := strings.Fields(optName)
for i, name := range res {
prefix := "-"
if len(name) > 1 {
prefix = "--"
}
namesSl[i] = prefix + name
res[i] = prefix + name
}
return namesSl
return res
}

func (c *Cmd) mkOpt(opt opt) {
Expand Down

0 comments on commit 82aefbe

Please sign in to comment.