Class: GitCompound::Command::Arguments::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/git_compound/command/arguments/parser.rb

Overview

Class responsible for parsing ARGV for given procedure

Instance Method Summary collapse

Constructor Details

#initialize(argv, global) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
# File 'lib/git_compound/command/arguments/parser.rb', line 7

def initialize(argv, global)
  @global = global
  @args   = format_arguments(argv)
end

Instance Method Details

#commandObject



39
40
41
# File 'lib/git_compound/command/arguments/parser.rb', line 39

def command
  @args.find { |arg| arg.is_a?(String) }
end

#globalObject



35
36
37
# File 'lib/git_compound/command/arguments/parser.rb', line 35

def global
  @args & @global
end

#optionsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/git_compound/command/arguments/parser.rb', line 18

def options
  arguments = @args - @global - [command]
  found = {}

  option_each(procedure.options) do |name, type|
    option = type.new(name, arguments)
    next unless option.valid?

    arguments -= option.used
    found.merge!(option.parse)
  end

  return found if arguments.empty?
  raise UnknownArgumentError,
        "Unknown arguments `#{arguments.inspect}`"
end

#procedureObject



12
13
14
15
16
# File 'lib/git_compound/command/arguments/parser.rb', line 12

def procedure
  Command.const_get("Procedure::#{command.capitalize}")
rescue
  Procedure::Help
end