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

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4 5
	"fmt"
	"strings"
6

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
	"github.com/jbenet/go-ipfs/commands"
8 9
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11
// Parse parses the input commandline string (cmd, flags, and args).
// returns the corresponding command Request object.
12
func Parse(input []string, root *commands.Command) (commands.Request, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
	path, input := parsePath(input, root)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15
	opts, args, err := parseOptions(input)
	if err != nil {
16
		return nil, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
	}
18

19
	return commands.NewRequest(path, opts, args, nil), nil
20 21
}

22
// parsePath gets the command path from the command line input
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23
func parsePath(input []string, root *commands.Command) ([]string, []string) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24 25
	cmd := root
	i := 0
26

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27 28 29 30
	for _, blob := range input {
		if strings.HasPrefix(blob, "-") {
			break
		}
31

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32
		cmd := cmd.Subcommand(blob)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33 34 35
		if cmd == nil {
			break
		}
36

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37 38
		i++
	}
39

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40
	return input[:i], input[i:]
41 42
}

43
// parseOptions parses the raw string values of the given options
44
// returns the parsed options as strings, along with the CLI args
45
func parseOptions(input []string) (map[string]interface{}, []string, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46 47
	opts := make(map[string]interface{})
	args := []string{}
48

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52 53 54
		if strings.HasPrefix(blob, "-") {
			name := blob[1:]
			value := ""
55

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56 57 58 59
			// support single and double dash
			if strings.HasPrefix(name, "-") {
				name = name[1:]
			}
60

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61 62 63 64 65
			if strings.Contains(name, "=") {
				split := strings.SplitN(name, "=", 2)
				name = split[0]
				value = split[1]
			}
66

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
			opts[name] = value
72

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74 75 76
		} else {
			args = append(args, blob)
		}
	}
77

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78
	return opts, args, nil
79
}