Class: GLI::GLIOptionParser::NormalCommandOptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/flapjack/patches.rb

Instance Method Summary collapse

Instance Method Details

#parse!(parsing_result, argument_handling_strategy) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/flapjack/patches.rb', line 19

def parse!(parsing_result,argument_handling_strategy)
  parsed_command_options = {}
  command = parsing_result.command
  arguments = nil

  loop do
    command._action.call if command.passthrough

    option_parser_factory       = OptionParserFactory.for_command(command,@accepts)
    option_block_parser         = CommandOptionBlockParser.new(option_parser_factory, self.error_handler)
    option_block_parser.command = command
    arguments                   = parsing_result.arguments

    arguments = option_block_parser.parse!(arguments)

    parsed_command_options[command] = option_parser_factory.options_hash_with_defaults_set!
    command_finder                  = CommandFinder.new(command.commands, :default_command => command.get_default_command)
    next_command_name               = arguments.shift

    verify_required_options!(command.flags, command, parsed_command_options[command])

    begin
      command = command_finder.find_command(next_command_name)
    rescue AmbiguousCommand
      arguments.unshift(next_command_name)
      break
    rescue UnknownCommand
      arguments.unshift(next_command_name)
      # Although command finder could certainly know if it should use
      # the default command, it has no way to put the "unknown command"
      # back into the argument stack.  UGH.
      unless command.get_default_command.nil?
        command = command_finder.find_command(command.get_default_command)
      end
      break
    end
  end

  parsed_command_options[command] ||= {}
  command_options = parsed_command_options[command]

  this_command          = command.parent
  child_command_options = command_options

  while this_command.kind_of?(command.class)
    this_command_options = parsed_command_options[this_command] || {}
    child_command_options[GLI::Command::PARENT] = this_command_options
    this_command = this_command.parent
    child_command_options = this_command_options
  end

  parsing_result.command_options = command_options
  parsing_result.command = command
  parsing_result.arguments = Array(arguments.compact)

  # Lets validate the arguments now that we know for sure the command that is invoked
  verify_arguments!(parsing_result.arguments, parsing_result.command) if argument_handling_strategy == :strict

  parsing_result
end