parse.go 3.88 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
// Parse will search each root to find the one that best matches the requested subcommand.
18
func Parse(input []string, root *cmds.Command) (cmds.Request, *cmds.Command, []string, error) {
19
	// use the root that matches the longest path (most accurately matches request)
20 21 22
	path, input, cmd := parsePath(input, root)
	opts, stringArgs, err := parseOptions(input)
	if err != nil {
23
		return nil, cmd, path, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24
	}
25

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

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

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

	req := cmds.NewRequest(path, opts, args, cmd, optDefs)
41 42 43

	err = cmd.CheckArguments(req)
	if err != nil {
44
		return req, cmd, path, err
45 46
	}

47
	return req, cmd, path, nil
48 49
}

50 51
// 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
52
func parsePath(input []string, root *cmds.Command) ([]string, []string, *cmds.Command) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53 54
	cmd := root
	i := 0
55

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56 57 58 59
	for _, blob := range input {
		if strings.HasPrefix(blob, "-") {
			break
		}
60

61 62
		sub := cmd.Subcommand(blob)
		if sub == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63 64
			break
		}
65
		cmd = sub
66

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67 68
		i++
	}
69

70
	return input[:i], input[i:], cmd
71 72
}

73
// parseOptions parses the raw string values of the given options
74
// returns the parsed options as strings, along with the CLI args
75
func parseOptions(input []string) (map[string]interface{}, []string, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76
	opts := make(map[string]interface{})
77
	args := []string{}
78

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82 83 84
		if strings.HasPrefix(blob, "-") {
			name := blob[1:]
			value := ""
85

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86 87 88 89
			// support single and double dash
			if strings.HasPrefix(name, "-") {
				name = name[1:]
			}
90

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91 92 93 94 95
			if strings.Contains(name, "=") {
				split := strings.SplitN(name, "=", 2)
				name = split[0]
				value = split[1]
			}
96

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101
			opts[name] = value
102

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103 104 105 106
		} else {
			args = append(args, blob)
		}
	}
107

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108
	return opts, args, nil
109
}
110

111
func parseArgs(stringArgs []string, arguments []cmds.Argument) ([]interface{}, error) {
112 113
	// count required argument definitions
	lenRequired := 0
114
	for _, argDef := range arguments {
115 116
		if argDef.Required {
			lenRequired++
117
		}
118
	}
119

120 121
	args := make([]interface{}, len(stringArgs))

122
	valueIndex := 0 // the index of the current stringArgs value
123
	for _, argDef := range arguments {
124
		// skip optional argument definitions if there aren't sufficient remaining values
125
		if len(stringArgs)-valueIndex <= lenRequired && !argDef.Required {
126
			continue
127 128
		} else if argDef.Required {
			lenRequired--
129
		}
130

131
		if valueIndex >= len(stringArgs) {
132 133 134 135
			break
		}

		if argDef.Variadic {
136
			for _, arg := range stringArgs[valueIndex:] {
137
				value, err := argValue(argDef, arg)
138 139 140
				if err != nil {
					return nil, err
				}
141
				args[valueIndex] = value
142
				valueIndex++
143
			}
144
		} else {
145
			var err error
146
			value, err := argValue(argDef, stringArgs[valueIndex])
147 148 149
			if err != nil {
				return nil, err
			}
150
			args[valueIndex] = value
151
			valueIndex++
152 153 154 155 156
		}
	}

	return args, nil
}
157

158
func argValue(argDef cmds.Argument, value string) (interface{}, error) {
159
	if argDef.Type == cmds.ArgString {
160
		return value, nil
161 162

	} else {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
163 164 165 166
		// NB At the time of this commit, file cleanup is performed when
		// Requests are cleaned up. TODO try to perform open and close at the
		// same level of abstraction (or at least in the same package!)
		in, err := os.Open(value)
167 168 169
		if err != nil {
			return nil, err
		}
170
		return in, nil
171 172
	}
}