Class: GLI::GLIOptionParser::NormalCommandOptionParser

Inherits:
GlobalOptionParser show all
Defined in:
lib/gli/gli_option_parser.rb

Direct Known Subclasses

LegacyCommandOptionParser

Instance Method Summary collapse

Constructor Details

#initialize(accepts) ⇒ NormalCommandOptionParser

Returns a new instance of NormalCommandOptionParser.



124
125
126
# File 'lib/gli/gli_option_parser.rb', line 124

def initialize(accepts)
  @accepts = accepts
end

Instance Method Details

#error_handlerObject



128
129
130
131
132
# File 'lib/gli/gli_option_parser.rb', line 128

def error_handler
  lambda { |message,extra_error_context|
    raise UnknownCommandArgument.new(message,extra_error_context)
  }
end

#parse!(parsing_result, argument_handling_strategy, autocomplete) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/gli/gli_option_parser.rb', line 134

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

  loop do
    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, :autocomplete => autocomplete)
    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