Skip to content

Commit

Permalink
commands/cli: Added path/args parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Oct 14, 2014
1 parent 3848a9c commit 8536ec0
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion commands/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ func Parse(input []string, root *commands.Command) ([]string, []string, map[stri
return nil, nil, nil, err
}

return nil, nil, opts, nil
path, args, err := path(input, root)
if err != nil {
return nil, nil, nil, err
}

return path, args, opts, nil
}

// options parses the raw string values of the given options
Expand Down Expand Up @@ -90,3 +95,21 @@ func options(input []string, root *commands.Command) (map[string]string, []strin

return opts, cleanInput, nil
}

// path takes the command line (without options) and splits it into the command path and arguments
func path(input []string, root *commands.Command) ([]string, []string, error) {
cmd := root
i := 0

for _, blob := range input {
cmd := cmd.Sub(blob)

if cmd == nil {
break
}

i++
}

return input[:i], input[i:], nil
}

0 comments on commit 8536ec0

Please sign in to comment.