Class: GLI::GLIOptionParser::GlobalOptionParser

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

Direct Known Subclasses

NormalCommandOptionParser

Instance Method Summary collapse

Constructor Details

#initialize(option_parser_factory, command_finder, flags, options = {}) ⇒ GlobalOptionParser

Returns a new instance of GlobalOptionParser.



39
40
41
42
43
44
# File 'lib/gli/gli_option_parser.rb', line 39

def initialize(option_parser_factory,command_finder,flags,options={})
  @option_parser_factory = option_parser_factory
  @command_finder        = command_finder
  @flags                 = flags
  @options               = options
end

Instance Method Details

#parse!(parsing_result) ⇒ Object



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
# File 'lib/gli/gli_option_parser.rb', line 46

def parse!(parsing_result)
  parsing_result.arguments      = GLIOptionBlockParser.new(@option_parser_factory,UnknownGlobalArgument).parse!(parsing_result.arguments)
  parsing_result.global_options = @option_parser_factory.options_hash_with_defaults_set!
  command_name = if parsing_result.global_options[:help] || parsing_result.global_options[:version]
                   "help"
                 else
                   parsing_result.arguments.shift
                 end
  parsing_result.command        = begin
    @command_finder.find_command(command_name)
  rescue UnknownCommand => e
    raise e unless @options[:command_missing_block]
    command = @options[:command_missing_block].call(command_name.to_sym,parsing_result.global_options)
    if command
      unless command.is_a?(Command)
        raise UnknownCommand.new("Expected the `command_missing` block to return a GLI::Command object, got a #{command.class.name} instead.")
      end
    else
      raise e
    end
    command
  end

  unless command_name == 'help'
    verify_required_options!(@flags, parsing_result.command, parsing_result.global_options)
  end
  parsing_result
end