Class: Mothership::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/mothership/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Parser

Returns a new instance of Parser.



3
4
5
# File 'lib/mothership/parser.rb', line 3

def initialize(command)
  @command = command
end

Instance Method Details

#inputs(argv) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/mothership/parser.rb', line 7

def inputs(argv)
  inputs = {}

  args = parse_flags(inputs, argv.dup)

  parse_arguments(inputs, args)

  inputs
end

#parse_arguments(inputs, args) ⇒ Object

FOO
BAR

FIZZ BUZZ:

1 2 => :fizz => 1, :buzz => 2
1 2 3 => :foo => 1, :fizz => 2, :buzz => 3
1 2 3 4 => :foo => 1, :bar => 2, :fizz => 3, :buzz => 4

Raises:



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
112
113
114
115
116
# File 'lib/mothership/parser.rb', line 71

def parse_arguments(inputs, args)
  total = @command.arguments.size
  required = 0
  optional = 0
  @command.arguments.each do |arg|
    case arg[:type]
    when :optional
      optional += 1
    when :splat
      break
    else
      required += 1
    end
  end

  parse_optionals = args.size - required

  @command.arguments.each do |arg|
    name = arg[:name]
    next if inputs.key? name

    case arg[:type]
    when :splat
      inputs[name] = []

      until args.empty?
        inputs[name] << args.shift
      end

    when :optional
      if parse_optionals > 0 && val = args.shift
        inputs[name] = val
        parse_optionals -= 1
      end

    else
      if val = args.shift
        inputs[name] = val
      elsif !@command.inputs[name][:default]
        raise MissingArgument.new(@command.name, name)
      end
    end
  end

  raise ExtraArguments.new(@command.name) unless args.empty?
end

#parse_flags(inputs, argv) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mothership/parser.rb', line 17

def parse_flags(inputs, argv)
  args = []

  until argv.empty?
    flag = normalize_flag(argv.shift, argv)

    name = @command.flags[flag]
    unless name
      args << flag
      next
    end

    input = @command.inputs[name]

    case input[:type]
    when :bool, :boolean
      if argv.first == "false" || argv.first == "true"
        inputs[name] = argv.shift == "true"
      else
        inputs[name] = true
      end
    when :float, :floating
      if !argv.empty? && argv.first =~ /^[0-9]+(\.[0-9]*)?$/
        inputs[name] = argv.shift.to_f
      else
        raise TypeMismatch.new(@command.name, name, "floating")
      end
    when :integer, :number, :numeric
      if !argv.empty? && argv.first =~ /^[0-9]+$/
        inputs[name] = argv.shift.to_i
      else
        raise TypeMismatch.new(@command.name, name, "numeric")
      end
    else
      if argv.empty? || !argv.first.start_with?("-")
        arg = argv.shift || ""

        inputs[name] =
          if input[:argument] == :splat
            arg.split(",")
          else
            arg
          end
      end
    end
  end

  args
end