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

Parse Fix #366

Merged
merged 2 commits into from
Nov 20, 2014
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
8 changes: 5 additions & 3 deletions commands/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
argDef := getArgDef(argDefIndex, argDefs)

// skip optional argument definitions if there aren't sufficient remaining inputs
if numInputs-i <= numRequired && !argDef.Required {
continue
} else if argDef.Required {
for numInputs-i <= numRequired && !argDef.Required {
argDefIndex++
argDef = getArgDef(argDefIndex, argDefs)
}
if argDef.Required {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this is the kind of subtle bug that i was worried about

numRequired--
}

Expand Down
105 changes: 105 additions & 0 deletions commands/cli/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,108 @@ func TestOptionParsing(t *testing.T) {
t.Errorf("Returned command was different than expected")
}
}

func TestArgumentParsing(t *testing.T) {
rootCmd := &commands.Command{
Subcommands: map[string]*commands.Command{
"noarg": &commands.Command{},
"onearg": &commands.Command{
Arguments: []commands.Argument{
commands.StringArg("a", true, false, "some arg"),
},
},
"twoargs": &commands.Command{
Arguments: []commands.Argument{
commands.StringArg("a", true, false, "some arg"),
commands.StringArg("b", true, false, "another arg"),
},
},
"variadic": &commands.Command{
Arguments: []commands.Argument{
commands.StringArg("a", true, true, "some arg"),
},
},
"optional": &commands.Command{
Arguments: []commands.Argument{
commands.StringArg("b", false, true, "another arg"),
},
},
"reversedoptional": &commands.Command{
Arguments: []commands.Argument{
commands.StringArg("a", false, false, "some arg"),
commands.StringArg("b", true, false, "another arg"),
},
},
},
}

_, _, _, err := Parse([]string{"noarg"}, nil, rootCmd)
if err != nil {
t.Error("Should have passed")
}
_, _, _, err = Parse([]string{"noarg", "value!"}, nil, rootCmd)
if err == nil {
t.Error("Should have failed (provided an arg, but command didn't define any)")
}

_, _, _, err = Parse([]string{"onearg", "value!"}, nil, rootCmd)
if err != nil {
t.Error("Should have passed")
}
_, _, _, err = Parse([]string{"onearg"}, nil, rootCmd)
if err == nil {
t.Error("Should have failed (didn't provide any args, arg is required)")
}

_, _, _, err = Parse([]string{"twoargs", "value1", "value2"}, nil, rootCmd)
if err != nil {
t.Error("Should have passed")
}
_, _, _, err = Parse([]string{"twoargs", "value!"}, nil, rootCmd)
if err == nil {
t.Error("Should have failed (only provided 1 arg, needs 2)")
}
_, _, _, err = Parse([]string{"twoargs"}, nil, rootCmd)
if err == nil {
t.Error("Should have failed (didn't provide any args, 2 required)")
}

_, _, _, err = Parse([]string{"variadic", "value!"}, nil, rootCmd)
if err != nil {
t.Error("Should have passed")
}
_, _, _, err = Parse([]string{"variadic", "value1", "value2", "value3"}, nil, rootCmd)
if err != nil {
t.Error("Should have passed")
}
_, _, _, err = Parse([]string{"variadic"}, nil, rootCmd)
if err == nil {
t.Error("Should have failed (didn't provide any args, 1 required)")
}

_, _, _, err = Parse([]string{"optional", "value!"}, nil, rootCmd)
if err != nil {
t.Error("Should have passed")
}
_, _, _, err = Parse([]string{"optional"}, nil, rootCmd)
if err != nil {
t.Error("Should have passed")
}

_, _, _, err = Parse([]string{"reversedoptional", "value1", "value2"}, nil, rootCmd)
if err != nil {
t.Error("Should have passed")
}
_, _, _, err = Parse([]string{"reversedoptional", "value!"}, nil, rootCmd)
if err != nil {
t.Error("Should have passed")
}
_, _, _, err = Parse([]string{"reversedoptional"}, nil, rootCmd)
if err == nil {
t.Error("Should have failed (didn't provide any args, 1 required)")
}
_, _, _, err = Parse([]string{"reversedoptional", "value1", "value2", "value3"}, nil, rootCmd)
if err == nil {
t.Error("Should have failed (provided too many args, only takes 1)")
}
}