parse.go 4.41 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 18
// Multiple root commands are supported:
// Parse will search each root to find the one that best matches the requested subcommand.
19 20
// 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
21
func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command, *cmds.Command, []string, error) {
22 23 24
	var root, cmd *cmds.Command
	var path, stringArgs []string
	var opts map[string]interface{}
25 26 27

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

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

46
	if maxLength == 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47
		return nil, root, nil, path, ErrInvalidSubcmd
48 49
	}

50 51
	args, err := parseArgs(stringArgs, cmd)
	if err != nil {
52
		return nil, root, cmd, path, err
53 54
	}

55 56 57 58 59 60
	optDefs, err := root.GetOptions(path)
	if err != nil {
		return nil, root, cmd, path, err
	}

	req := cmds.NewRequest(path, opts, args, cmd, optDefs)
61 62 63

	err = cmd.CheckArguments(req)
	if err != nil {
64
		return req, root, cmd, path, err
65 66
	}

67
	return req, root, cmd, path, nil
68 69
}

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87 88
		i++
	}
89

90
	return input[:i], input[i:], cmd
91 92
}

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

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

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

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
121
			opts[name] = value
122

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
123 124 125 126
		} else {
			args = append(args, blob)
		}
	}
127

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
128
	return opts, args, nil
129
}
130 131

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

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

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

151
		if valueIndex >= len(stringArgs) {
152 153 154 155
			break
		}

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

174 175
	if len(stringArgs)-valueIndex > 0 {
		args = append(args, make([]interface{}, len(stringArgs)-valueIndex))
176 177
	}

178 179
	return args, nil
}
180 181 182 183 184 185

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

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