parse.go 4.05 KB
Newer Older
1 2 3
package cli

import (
4
	"errors"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	"fmt"
6
	"os"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
	"strings"
8

9
	cmds "github.com/jbenet/go-ipfs/commands"
10 11
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13 14
// ErrInvalidSubcmd signals when the parse error is not found
var ErrInvalidSubcmd = errors.New("subcommand not found")

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15 16
// Parse parses the input commandline string (cmd, flags, and args).
// returns the corresponding command Request object.
17
// Parse will search each root to find the one that best matches the requested subcommand.
18 19
// TODO: get rid of extraneous return values (e.g. we ended up not needing the root value anymore)
// TODO: get rid of multiple-root support, we should only need one now
20
func Parse(input []string, root *cmds.Command) (cmds.Request, *cmds.Command, *cmds.Command, []string, error) {
21
	// use the root that matches the longest path (most accurately matches request)
22 23 24 25
	path, input, cmd := parsePath(input, root)
	opts, stringArgs, err := parseOptions(input)
	if err != nil {
		return nil, root, cmd, path, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26
	}
27

28
	if len(path) == 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29
		return nil, root, nil, path, ErrInvalidSubcmd
30 31
	}

32 33
	args, err := parseArgs(stringArgs, cmd)
	if err != nil {
34
		return nil, root, cmd, path, err
35 36
	}

37 38 39 40 41 42
	optDefs, err := root.GetOptions(path)
	if err != nil {
		return nil, root, cmd, path, err
	}

	req := cmds.NewRequest(path, opts, args, cmd, optDefs)
43 44 45

	err = cmd.CheckArguments(req)
	if err != nil {
46
		return req, root, cmd, path, err
47 48
	}

49
	return req, root, cmd, path, nil
50 51
}

52 53
// parsePath separates the command path and the opts and args from a command string
// returns command path slice, rest slice, and the corresponding *cmd.Command
54
func parsePath(input []string, root *cmds.Command) ([]string, []string, *cmds.Command) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56
	cmd := root
	i := 0
57

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58 59 60 61
	for _, blob := range input {
		if strings.HasPrefix(blob, "-") {
			break
		}
62

63 64
		sub := cmd.Subcommand(blob)
		if sub == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65 66
			break
		}
67
		cmd = sub
68

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69 70
		i++
	}
71

72
	return input[:i], input[i:], cmd
73 74
}

75
// parseOptions parses the raw string values of the given options
76
// returns the parsed options as strings, along with the CLI args
77
func parseOptions(input []string) (map[string]interface{}, []string, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78
	opts := make(map[string]interface{})
79
	args := []string{}
80

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81 82
	for i := 0; i < len(input); i++ {
		blob := input[i]
83

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84 85 86
		if strings.HasPrefix(blob, "-") {
			name := blob[1:]
			value := ""
87

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
88 89 90 91
			// support single and double dash
			if strings.HasPrefix(name, "-") {
				name = name[1:]
			}
92

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93 94 95 96 97
			if strings.Contains(name, "=") {
				split := strings.SplitN(name, "=", 2)
				name = split[0]
				value = split[1]
			}
98

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99 100 101
			if _, ok := opts[name]; ok {
				return nil, nil, fmt.Errorf("Duplicate values for option '%s'", name)
			}
102

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103
			opts[name] = value
104

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105 106 107 108
		} else {
			args = append(args, blob)
		}
	}
109

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110
	return opts, args, nil
111
}
112 113

func parseArgs(stringArgs []string, cmd *cmds.Command) ([]interface{}, error) {
114
	args := make([]interface{}, 0)
115

116 117 118 119 120
	// count required argument definitions
	lenRequired := 0
	for _, argDef := range cmd.Arguments {
		if argDef.Required {
			lenRequired++
121
		}
122
	}
123

124
	valueIndex := 0 // the index of the current stringArgs value
125 126
	for _, argDef := range cmd.Arguments {
		// skip optional argument definitions if there aren't sufficient remaining values
127
		if len(stringArgs)-valueIndex <= lenRequired && !argDef.Required {
128
			continue
129 130
		} else if argDef.Required {
			lenRequired--
131
		}
132

133
		if valueIndex >= len(stringArgs) {
134 135 136 137
			break
		}

		if argDef.Variadic {
138
			for _, arg := range stringArgs[valueIndex:] {
139 140 141 142 143
				var err error
				args, err = appendArg(args, argDef, arg)
				if err != nil {
					return nil, err
				}
144
				valueIndex++
145
			}
146
		} else {
147
			var err error
148
			args, err = appendArg(args, argDef, stringArgs[valueIndex])
149 150 151
			if err != nil {
				return nil, err
			}
152
			valueIndex++
153 154 155
		}
	}

156 157
	if len(stringArgs)-valueIndex > 0 {
		args = append(args, make([]interface{}, len(stringArgs)-valueIndex))
158 159
	}

160 161
	return args, nil
}
162 163 164 165 166 167

func appendArg(args []interface{}, argDef cmds.Argument, value string) ([]interface{}, error) {
	if argDef.Type == cmds.ArgString {
		return append(args, value), nil

	} else {
168
		in, err := os.Open(value) // FIXME(btc) must close file. fix before merge
169 170 171 172 173 174
		if err != nil {
			return nil, err
		}
		return append(args, in), nil
	}
}