parse.go 4.32 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
// Parse parses the input commandline string (cmd, flags, and args).
// returns the corresponding command Request object.
14 15
// Multiple root commands are supported:
// Parse will search each root to find the one that best matches the requested subcommand.
16 17
// 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
18
func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command, *cmds.Command, []string, error) {
19 20 21
	var root, cmd *cmds.Command
	var path, stringArgs []string
	var opts map[string]interface{}
22 23 24

	// use the root that matches the longest path (most accurately matches request)
	maxLength := 0
25 26 27
	for _, root2 := range roots {
		path2, input2, cmd2 := parsePath(input, root2)
		opts2, stringArgs2, err := parseOptions(input2)
28
		if err != nil {
29
			return nil, root, cmd2, path2, err
30 31
		}

32
		length := len(path2)
33 34
		if length > maxLength {
			maxLength = length
35 36 37 38 39
			root = root2
			path = path2
			cmd = cmd2
			opts = opts2
			stringArgs = stringArgs2
40
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
	}
42

43
	if maxLength == 0 {
44
		return nil, root, nil, path, errors.New("Not a valid subcommand")
45 46
	}

47 48
	args, err := parseArgs(stringArgs, cmd)
	if err != nil {
49
		return nil, root, cmd, path, err
50 51
	}

52 53 54 55 56 57
	optDefs, err := root.GetOptions(path)
	if err != nil {
		return nil, root, cmd, path, err
	}

	req := cmds.NewRequest(path, opts, args, cmd, optDefs)
58 59 60

	err = cmd.CheckArguments(req)
	if err != nil {
61
		return req, root, cmd, path, err
62 63
	}

64
	return req, root, cmd, path, nil
65 66
}

67 68
// 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
69
func parsePath(input []string, root *cmds.Command) ([]string, []string, *cmds.Command) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70 71
	cmd := root
	i := 0
72

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74 75 76
	for _, blob := range input {
		if strings.HasPrefix(blob, "-") {
			break
		}
77

78 79
		sub := cmd.Subcommand(blob)
		if sub == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81
			break
		}
82
		cmd = sub
83

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84 85
		i++
	}
86

87
	return input[:i], input[i:], cmd
88 89
}

90
// parseOptions parses the raw string values of the given options
91
// returns the parsed options as strings, along with the CLI args
92
func parseOptions(input []string) (map[string]interface{}, []string, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93
	opts := make(map[string]interface{})
94
	args := []string{}
95

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99 100 101
		if strings.HasPrefix(blob, "-") {
			name := blob[1:]
			value := ""
102

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103 104 105 106
			// support single and double dash
			if strings.HasPrefix(name, "-") {
				name = name[1:]
			}
107

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108 109 110 111 112
			if strings.Contains(name, "=") {
				split := strings.SplitN(name, "=", 2)
				name = split[0]
				value = split[1]
			}
113

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
118
			opts[name] = value
119

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
120 121 122 123
		} else {
			args = append(args, blob)
		}
	}
124

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
125
	return opts, args, nil
126
}
127 128

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

131 132 133 134 135
	// count required argument definitions
	lenRequired := 0
	for _, argDef := range cmd.Arguments {
		if argDef.Required {
			lenRequired++
136
		}
137
	}
138

139
	valueIndex := 0 // the index of the current stringArgs value
140 141
	for _, argDef := range cmd.Arguments {
		// skip optional argument definitions if there aren't sufficient remaining values
142
		if len(stringArgs)-valueIndex <= lenRequired && !argDef.Required {
143
			continue
144 145
		} else if argDef.Required {
			lenRequired--
146
		}
147

148
		if valueIndex >= len(stringArgs) {
149 150 151 152
			break
		}

		if argDef.Variadic {
153
			for _, arg := range stringArgs[valueIndex:] {
154 155 156 157 158
				var err error
				args, err = appendArg(args, argDef, arg)
				if err != nil {
					return nil, err
				}
159
				valueIndex++
160
			}
161
		} else {
162
			var err error
163
			args, err = appendArg(args, argDef, stringArgs[valueIndex])
164 165 166
			if err != nil {
				return nil, err
			}
167
			valueIndex++
168 169 170
		}
	}

171 172
	if len(stringArgs)-valueIndex > 0 {
		args = append(args, make([]interface{}, len(stringArgs)-valueIndex))
173 174
	}

175 176
	return args, nil
}
177 178 179 180 181 182

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

	} else {
183
		in, err := os.Open(value) // FIXME(btc) must close file. fix before merge
184 185 186 187 188 189
		if err != nil {
			return nil, err
		}
		return append(args, in), nil
	}
}