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 issue #275 #276

Merged
merged 3 commits into from
Sep 27, 2018
Merged
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
68 changes: 68 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,71 @@ func TestSubCommandFindOptionByShortFlag(t *testing.T) {
t.Errorf("Expected 'o', but got %v", opt.ShortName)
}
}

type fooCmd struct {
Flag bool `short:"f"`
args []string
}

func (foo *fooCmd) Execute(s []string) error {
foo.args = s
return nil
}

func TestCommandPassAfterNonOption(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Foo fooCmd `command:"foo"`
}{}
p := NewParser(&opts, PassAfterNonOption)
ret, err := p.ParseArgs([]string{"-v", "foo", "-f", "bar", "-v", "-g"})

if err != nil {
t.Fatalf("Unexpected error: %v", err)
return
}

if !opts.Value {
t.Errorf("Expected Value to be true")
}

if !opts.Foo.Flag {
t.Errorf("Expected Foo.Flag to be true")
}

assertStringArray(t, ret, []string{"bar", "-v", "-g"})
assertStringArray(t, opts.Foo.args, []string{"bar", "-v", "-g"})
}

type barCmd struct {
fooCmd
Positional struct {
Args []string
} `positional-args:"yes"`
}

func TestCommandPassAfterNonOptionWithPositional(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Bar barCmd `command:"bar"`
}{}
p := NewParser(&opts, PassAfterNonOption)
ret, err := p.ParseArgs([]string{"-v", "bar", "-f", "baz", "-v", "-g"})

if err != nil {
t.Fatalf("Unexpected error: %v", err)
return
}

if !opts.Value {
t.Errorf("Expected Value to be true")
}

if !opts.Bar.Flag {
t.Errorf("Expected Bar.Flag to be true")
}

assertStringArray(t, ret, []string{})
assertStringArray(t, opts.Bar.args, []string{})
assertStringArray(t, opts.Bar.Positional.Args, []string{"baz", "-v", "-g"})
}
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (p *Parser) ParseArgs(args []string) ([]string, error) {
}

if !argumentIsOption(arg) {
if (p.Options & PassAfterNonOption) != None {
if (p.Options&PassAfterNonOption) != None && s.lookup.commands[arg] == nil {
// If PassAfterNonOption is set then all remaining arguments
// are considered positional
if err = s.addArgs(s.arg); err != nil {
Expand Down