Skip to content

Commit

Permalink
Fix long short flags and test (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
pballandras authored Feb 28, 2023
1 parent 8069c86 commit 9bae5f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ func (p *ParseContext) Next() *Token {
}

if strings.HasPrefix(arg, "--") || (strings.HasPrefix(arg, "-") && strings.Count(arg, "-") > 1) {
parts := strings.SplitN(arg[2:], "=", 2)
var parts []string
if strings.HasPrefix(arg, "--") {
parts = strings.SplitN(arg[2:], "=", 2)
} else {
parts = strings.SplitN(arg[1:], "=", 2)
}
token := &Token{p.argi, TokenLong, parts[0]}
if len(parts) == 2 {
p.Push(&Token{p.argi, TokenArg, parts[1]})
Expand Down
11 changes: 11 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,14 @@ func TestAppParseUnmanagedVarWithTwoManagedFlags(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, []string{"-var"}, app.Unmanaged)
}

func TestAppParseShortLongFlags(t *testing.T) {
app := New("test", "")
app.allowUnmanaged = true
app.Command("foo", "")
app.Flag("verbose-level", "").Short('v').Bool()

ctx, err := app.ParseContext([]string{"foo", "-verbose-level"})
assert.Nil(t, err)
assert.Len(t, ctx.Elements, 2)
}

0 comments on commit 9bae5f8

Please sign in to comment.