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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14 15
// 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
16 17
// Parse parses the input commandline string (cmd, flags, and args).
// returns the corresponding command Request object.
18
// Parse will search each root to find the one that best matches the requested subcommand.
19
func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *cmds.Command, []string, error) {
20
	// use the root that matches the longest path (most accurately matches request)
21 22 23
	path, input, cmd := parsePath(input, root)
	opts, stringArgs, err := parseOptions(input)
	if err != nil {
24
		return nil, cmd, path, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25
	}
26

27
	if len(path) == 0 {
28
		return nil, nil, path, ErrInvalidSubcmd
29 30
	}

31
	args, err := parseArgs(stringArgs, stdin, cmd.Arguments)
32
	if err != nil {
33
		return nil, cmd, path, err
34 35
	}

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

41 42 43 44 45 46 47 48
	// check to make sure there aren't any undefined options
	for k := range opts {
		if _, found := optDefs[k]; !found {
			err = fmt.Errorf("Unrecognized option: -%s", k)
			return nil, cmd, path, err
		}
	}

49
	req := cmds.NewRequest(path, opts, args, cmd, optDefs)
50 51 52

	err = cmd.CheckArguments(req)
	if err != nil {
53
		return req, cmd, path, err
54 55
	}

56
	return req, cmd, path, nil
57 58
}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65 66 67 68
	for _, blob := range input {
		if strings.HasPrefix(blob, "-") {
			break
		}
69

70 71
		sub := cmd.Subcommand(blob)
		if sub == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72 73
			break
		}
74
		cmd = sub
75

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76 77
		i++
	}
78

79
	return input[:i], input[i:], cmd
80 81
}

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91 92 93
		if strings.HasPrefix(blob, "-") {
			name := blob[1:]
			value := ""
94

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
95 96 97 98
			// support single and double dash
			if strings.HasPrefix(name, "-") {
				name = name[1:]
			}
99

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100 101 102 103 104
			if strings.Contains(name, "=") {
				split := strings.SplitN(name, "=", 2)
				name = split[0]
				value = split[1]
			}
105

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110
			opts[name] = value
111

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
112 113 114 115
		} else {
			args = append(args, blob)
		}
	}
116

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117
	return opts, args, nil
118
}
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
func parseArgs(stringArgs []string, stdin *os.File, arguments []cmds.Argument) ([]interface{}, error) {
	// check if stdin is coming from terminal or is being piped in
	if stdin != nil {
		stat, err := stdin.Stat()
		if err != nil {
			return nil, err
		}

		// if stdin isn't a CharDevice, set it to nil
		// (this means it is coming from terminal and we want to ignore it)
		if (stat.Mode() & os.ModeCharDevice) != 0 {
			stdin = nil
		}
	}

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

143 144 145 146 147 148 149 150 151 152 153 154 155 156
	valCount := len(stringArgs)
	if stdin != nil {
		valCount += 1
	}

	args := make([]interface{}, 0, valCount)

	argDefIndex := 0 // the index of the current argument definition
	for i := 0; i < valCount; i++ {
		// get the argument definiton (should be arguments[argDefIndex],
		// but if argDefIndex > len(arguments) we use the last argument definition)
		var argDef cmds.Argument
		if argDefIndex < len(arguments) {
			argDef = arguments[argDefIndex]
157
		} else if len(arguments) > 0 {
158 159
			argDef = arguments[len(arguments)-1]
		}
160

161
		// skip optional argument definitions if there aren't sufficient remaining values
162
		if valCount-i <= lenRequired && !argDef.Required {
163
			continue
164 165
		} else if argDef.Required {
			lenRequired--
166
		}
167

168 169 170 171 172
		if argDef.Type == cmds.ArgString {
			if stdin == nil {
				// add string values
				args = append(args, stringArgs[0])
				stringArgs = stringArgs[1:]
173

174 175 176 177
			} else {
				// if we have a stdin, read it in and use the data as a string value
				var buf bytes.Buffer
				_, err := buf.ReadFrom(stdin)
178 179 180
				if err != nil {
					return nil, err
				}
181 182
				args = append(args, buf.String())
				stdin = nil
183
			}
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

		} else if argDef.Type == cmds.ArgFile {
			if stdin == nil {
				// treat stringArg values as file paths
				file, err := os.Open(stringArgs[0])
				if err != nil {
					return nil, err
				}
				args = append(args, file)
				stringArgs = stringArgs[1:]

			} else {
				// if we have a stdin, use that as a reader
				args = append(args, stdin)
				stdin = nil
199 200
			}
		}
201 202

		argDefIndex++
203 204 205 206
	}

	return args, nil
}