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

	// use the root that matches the longest path (most accurately matches request)
	maxLength := 0
	for _, r := range roots {
22 23
		p, i, c := parsePath(input, r)
		o, s, err := parseOptions(i)
24
		if err != nil {
25
			return nil, root, c, p, err
26 27
		}

28
		length := len(p)
29 30 31
		if length > maxLength {
			maxLength = length
			root = r
32 33 34 35
			path = p
			cmd = c
			opts = o
			stringArgs = s
36
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37
	}
38

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

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

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

	req := cmds.NewRequest(path, opts, args, cmd, optDefs)
54 55 56

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

60
	return req, root, cmd, path, nil
61 62
}

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81
		i++
	}
82

83
	return input[:i], input[i:], cmd
84 85
}

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

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

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

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
114
			opts[name] = value
115

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

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

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

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

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

144 145 146 147 148 149 150 151 152 153 154
		if j >= len(stringArgs) {
			break
		}

		if argDef.Variadic {
			for _, arg := range stringArgs[j:] {
				var err error
				args, err = appendArg(args, argDef, arg)
				if err != nil {
					return nil, err
				}
Matt Bell's avatar
Matt Bell committed
155
				j++
156
			}
157
		} else {
158 159
			var err error
			args, err = appendArg(args, argDef, stringArgs[j])
160 161 162
			if err != nil {
				return nil, err
			}
Matt Bell's avatar
Matt Bell committed
163
			j++
164 165 166
		}
	}

167 168 169 170
	if len(stringArgs)-j > 0 {
		args = append(args, make([]interface{}, len(stringArgs)-j))
	}

171 172
	return args, nil
}
173 174 175 176 177 178 179 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 {
		in, err := os.Open(value)
		if err != nil {
			return nil, err
		}
		return append(args, in), nil
	}
}