Skip to content

Commit

Permalink
Fix issue jessevdk#275
Browse files Browse the repository at this point in the history
This change restores the old behaviour of PassAfterNonOption working
together with commands so that a command would (also) get everything
after the first non-option, while also preserving the new behaviour of
those non-options landing in the positional array if provided.

The behaviour for when a command and a positional array are both
present continues to be weird (but it's unclear whether there's a
non-weird way out of that one other than failing with a "don't do
that").
  • Loading branch information
chipaca committed Sep 26, 2018
1 parent 20337d3 commit fb5b1ac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ func TestPassAfterNonOption(t *testing.T) {
assertStringArray(t, ret, []string{"arg", "-v", "-g"})
}

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

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

func TestPassAfterNonOptionWithCommand(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"})
}

func TestPassAfterNonOptionWithPositional(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Expand Down
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

0 comments on commit fb5b1ac

Please sign in to comment.