parse.go 2.67 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, 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 25 26 27
		if err != nil {
			return nil, nil, err
		}

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 40 41 42
	if maxLength == 0 {
		return nil, nil, errors.New("Not a valid subcommand")
	}

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

48 49 50 51 52 53 54 55
	req := cmds.NewRequest(path, opts, args, cmd)

	err = cmd.CheckArguments(req)
	if err != nil {
		return nil, nil, err
	}

	return req, root, nil
56 57
}

58
// parsePath gets the command path from the command line input
59
func parsePath(input []string, root *cmds.Command) ([]string, []string, *cmds.Command) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60 61
	cmd := root
	i := 0
62

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74 75
		i++
	}
76

77
	return input[:i], input[i:], cmd
78 79
}

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

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

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

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108
			opts[name] = value
109

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

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

func parseArgs(stringArgs []string, cmd *cmds.Command) ([]interface{}, error) {
119 120
	var argDef cmds.Argument
	args := make([]interface{}, len(stringArgs))
121

122 123 124
	for i, arg := range stringArgs {
		if i < len(cmd.Arguments) {
			argDef = cmd.Arguments[i]
125 126
		}

127 128
		if argDef.Type == cmds.ArgString {
			args[i] = arg
129 130

		} else {
131
			in, err := os.Open(arg)
132 133 134 135 136 137 138 139 140
			if err != nil {
				return nil, err
			}
			args[i] = in
		}
	}

	return args, nil
}