1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-06 11:31:54 +08:00

test(commands/parse) take args instead of cmd for easier testing

@mappum

License: MIT
Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
This commit is contained in:
Brian Tiger Chow
2014-11-12 23:44:16 -08:00
committed by Juan Batiz-Benet
parent 28306a49c4
commit 562500491f

View File

@ -27,7 +27,7 @@ func Parse(input []string, root *cmds.Command) (cmds.Request, *cmds.Command, []s
return nil, nil, path, ErrInvalidSubcmd
}
args, err := parseArgs(stringArgs, cmd)
args, err := parseArgs(stringArgs, cmd.Arguments)
if err != nil {
return nil, cmd, path, err
}
@ -108,10 +108,10 @@ func parseOptions(input []string) (map[string]interface{}, []string, error) {
return opts, args, nil
}
func parseArgs(stringArgs []string, cmd *cmds.Command) ([]interface{}, error) {
func parseArgs(stringArgs []string, arguments []cmds.Argument) ([]interface{}, error) {
// count required argument definitions
lenRequired := 0
for _, argDef := range cmd.Arguments {
for _, argDef := range arguments {
if argDef.Required {
lenRequired++
}
@ -120,7 +120,7 @@ func parseArgs(stringArgs []string, cmd *cmds.Command) ([]interface{}, error) {
args := make([]interface{}, len(stringArgs))
valueIndex := 0 // the index of the current stringArgs value
for _, argDef := range cmd.Arguments {
for _, argDef := range arguments {
// skip optional argument definitions if there aren't sufficient remaining values
if len(stringArgs)-valueIndex <= lenRequired && !argDef.Required {
continue