Always match commands before arguments.

This commit is contained in:
Alec Thomas
2018-06-08 14:27:08 +10:00
parent 01fa22ca03
commit 71b2dec48b
+15 -14
View File
@@ -264,22 +264,23 @@ func (c *Context) trace(node *Node) (err error) { // nolint: gocyclo
break break
} }
// After positional arguments have been consumed, handle commands and branching arguments. // After positional arguments have been consumed, check commands next...
for _, branch := range node.Children { for _, branch := range node.Children {
switch branch.Type { if branch.Type == CommandNode && branch.Name == token.Value {
case CommandNode: c.scan.Pop()
if branch.Name == token.Value { c.Path = append(c.Path, &Path{
c.scan.Pop() Parent: node,
c.Path = append(c.Path, &Path{ Command: branch,
Parent: node, Value: branch.Target,
Command: branch, Flags: node.Flags,
Value: branch.Target, })
Flags: node.Flags, return c.trace(branch)
}) }
return c.trace(branch) }
}
case ArgumentNode: // Finally, check arguments.
for _, branch := range node.Children {
if branch.Type == ArgumentNode {
arg := branch.Argument arg := branch.Argument
if value, err := arg.Parse(c.scan); err == nil { if value, err := arg.Parse(c.scan); err == nil {
c.Path = append(c.Path, &Path{ c.Path = append(c.Path, &Path{