parse.go 3.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package cli

import (
  "strings"
  "fmt"

  "github.com/jbenet/go-ipfs/commands"
)

func Parse(input []string, root *commands.Command) ([]string, []string, map[string]string, error) {
11
  path, input, err := parsePath(input, root)
12 13 14 15
  if err != nil {
    return nil, nil, nil, err
  }

16
  opts, args, err := parseOptions(input, path, root)
17 18 19 20 21
  if err != nil {
    return nil, nil, nil, err
  }

  return path, args, opts, nil
22 23
}

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

// path gets the command path from the command line input
func parsePath(input []string, root *commands.Command) ([]string, []string, error) {
  cmd := root
  i := 0

  for _, blob := range input {
    if strings.HasPrefix(blob, "-") {
      break
    }

    cmd := cmd.Sub(blob)
    if cmd == nil {
      break
    }

    i++
  }

  return input[:i], input[i:], nil
}

46
// options parses the raw string values of the given options
47 48
// returns the parsed options as strings, along with the CLI args
func parseOptions(input, path []string, root *commands.Command) (map[string]string, []string, error) {
49
  options, err := root.GetOptions(path)
50 51 52 53
  if err != nil {
    return nil, nil, err
  }

54
  opts := make(map[string]string)
55
  args := make([]string, 0)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

  // TODO: error if one option is defined multiple times

  for i := 0; i < len(input); i++ {
    blob := input[i]

    if strings.HasPrefix(blob, "--") {
      name := blob[2:]
      value := ""

      if strings.Contains(name, "=") {
        split := strings.SplitN(name, "=", 2)
        name = split[0]
        value = split[1]
      }

      if strings.Contains(name, "-") {
        return nil, nil, fmt.Errorf("Invalid option blob: '%s' (Shouldn't contain '-')", input[i])
      }

      if value != "" && strings.Contains(value, "\"") {
        // TODO: ignore escaped quotations (--foo="\"")
        if !strings.HasPrefix(value, "\"") {
          return nil, nil, fmt.Errorf("Invalid option blob: '%s' (Quotation wasn't at the start of value)", input[i])
        }

        value = value[1:]

        for {
          if strings.HasSuffix(value, "\"") {
            value = value[:len(value)-1]
            break
          }

          i++
          if i >= len(input) {
            return nil, nil, fmt.Errorf("Unterminated string: '%s'", value)
          }

          value += " " + input[i]
        }

        if strings.Contains(value, "\"") {
          return nil, nil, fmt.Errorf("Invalid option blob: '%s' (Value contains unescaped quotation)", value)
        }
      }

      opts[name] = value

    } else if strings.HasPrefix(blob, "-") {
      blob = blob[1:]

      if strings.ContainsAny(blob, "-=\"") {
        return nil, nil, fmt.Errorf("Invalid option blob: '%s'", input[i])
      }

112
      nameS := ""
113
      for _, name := range blob {
114 115
        nameS = string(name)
        opts[nameS] = ""
116 117
      }

118 119 120 121 122 123 124 125 126
      if nameS != "" {
        opt, ok := options[nameS]
        if ok && opt.Type != commands.Bool {
          i++
          if i <= len(input) {
            opts[nameS] = input[i]
          }
        }
      }
127 128

    } else {
129
      args = append(args, blob)
130 131 132
    }
  }

133
  return opts, args, nil
134
}