Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple short options with IgnoreUnknown option #400

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,8 @@ func (p *Parser) parseShort(s *parseState, optname string, argument *string) err
optname, argument = p.splitShortConcatArg(s, optname)
}

var err error

for i, c := range optname {
shortname := string(c)

Expand All @@ -641,15 +643,15 @@ func (p *Parser) parseShort(s *parseState, optname string, argument *string) err
return err
}
} else {
return newErrorf(ErrUnknownFlag, "unknown flag `%s'", shortname)
err = newErrorf(ErrUnknownFlag, "unknown flag `%s'", shortname)
}

// Only the first option can have a concatted argument, so just
// clear argument here
argument = nil
}

return nil
return err
}

func (p *parseState) addArgs(args ...string) error {
Expand Down
43 changes: 43 additions & 0 deletions unknown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,46 @@ func TestIgnoreUnknownFlags(t *testing.T) {
t.Fatalf("Expected %v but got %v", exargs, args)
}
}

func TestIgnoreUnknownStacked(t *testing.T) {
var opts = struct {
First bool `short:"f" long:"first"`
Second bool `short:"s" long:"second"`
}{}

args := []string{"-fas"}

p := NewParser(&opts, IgnoreUnknown)
args, err := p.ParseArgs(args)

exargs := []string{
"-fas",
}

if !opts.First {
t.Fatal("First arguement not recognized")
}

if !opts.Second {
t.Fatal("Second arguement not recognized")
}

if err != nil {
t.Fatal("Should not receive an error")
}

issame := (len(args) == len(exargs))

if issame {
for i := 0; i < len(args); i++ {
if args[i] != exargs[i] {
issame = false
break
}
}
}

if !issame {
t.Fatalf("Expected %v but got %v", exargs, args)
}
}