Skip to content

Commit

Permalink
fix: option with arguments bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
WindomZ committed Apr 19, 2017
1 parent 5bec42e commit e532043
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
6 changes: 5 additions & 1 deletion arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ func (a _Arguments) IsEmpty() bool {
func (a *_Arguments) Set(usage string) {
*a = (*a)[:0]
if strs := regexpArgument(usage); len(strs) != 0 {
m := make(map[string]bool, len(strs))
for _, str := range strs {
*a = append(*a, newArgument(str))
if _, ok := m[str]; !ok {
*a = append(*a, newArgument(str))
}
m[str] = true
}
}
}
Expand Down
31 changes: 20 additions & 11 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,28 @@ func (o *_Option) Aliases(aliases []string) *_Option {
return o
}

func (o _Option) UsageString(ones ...bool) (s string) {
s = o.usage
if ok, _ := regexp.MatchString(`^[\[(].+[)\]]$`, o.usage); ok {
} else if o.line && (len(ones) != 0 && ones[0]) {
} else if o.line || o.IsRequired() {
if len(o.names) > 1 {
s = fmt.Sprintf("(%s)", o.usage)
func (o _Option) UsageString(ones ...bool) string {
if ok, _ := regexp.MatchString(`^[\[(].+[)\]]$`, o.usage); !ok || len(o.names) > 1 {
strs := make([]string, len(o.names))
arg := strings.Join(o.arguments.Get(), " ")
for i, name := range o.names {
if len(arg) != 0 {
strs[i] = name + "=" + arg
} else {
strs[i] = name
}
}
} else if len(o.names) > 1 {
s = fmt.Sprintf("[%s]", o.usage)
s := strings.Join(strs, "|")

if o.line && len(ones) != 0 && ones[0] {
} else if o.line || o.IsRequired() {
s = fmt.Sprintf("(%s)", s)
} else {
s = fmt.Sprintf("[%s]", s)
}
return s
}
s = regexp.MustCompile(`(\s*,\s*-)|(\s-)`).ReplaceAllString(s, "|-")
return
return regexp.MustCompile(`(\s*,\s*-)|(\s-)`).ReplaceAllString(o.usage, "|-")
}

func (o _Option) OptionString() (s string) {
Expand Down
4 changes: 2 additions & 2 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestOption_1(t *testing.T) {
assert.Equal(t, o.Names(), []string{"-p"})
assert.Equal(t, o.IsRequired(), false)
assert.Equal(t, o.IsOptional(), true)
assert.Equal(t, o.UsageString(), "-p")
assert.Equal(t, o.UsageString(), "[-p]")
assert.Equal(t, o.OptionString(), "-p add pepper")
}

Expand Down Expand Up @@ -51,7 +51,7 @@ func TestOption_5(t *testing.T) {
assert.Equal(t, o.Names(), []string{"-p"})
assert.Equal(t, o.IsRequired(), false)
assert.Equal(t, o.IsOptional(), true)
assert.Equal(t, o.UsageString(), "-p=[path]")
assert.Equal(t, o.UsageString(), "[-p=[path]]")
assert.Equal(t, o.OptionString(), "-p=[path] add pepper directory [default: xxx]")
}

Expand Down

0 comments on commit e532043

Please sign in to comment.