parse.go 4.15 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
func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command, *cmds.Command, []string, error) {
17 18 19
	var root, cmd *cmds.Command
	var path, stringArgs []string
	var opts map[string]interface{}
20 21 22

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

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

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

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

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

	req := cmds.NewRequest(path, opts, args, cmd, optDefs)
56 57 58

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

62
	return req, root, cmd, path, nil
63 64
}

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82 83
		i++
	}
84

85
	return input[:i], input[i:], cmd
86 87
}

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

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

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

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
116
			opts[name] = value
117

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

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

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

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

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

146
		if valueIndex >= len(stringArgs) {
147 148 149 150
			break
		}

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

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

173 174
	return args, nil
}
175 176 177 178 179 180

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

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