Class: GLI::GLIOptionParser

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

Overview

Parses the command-line options using an actual OptionParser

Defined Under Namespace

Classes: GlobalOptionParser, LegacyCommandOptionParser, NormalCommandOptionParser

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :default_command => nil,
  :autocomplete => true,
  :subcommand_option_handling_strategy => :legacy,
  :argument_handling_strategy => :loose
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands, flags, switches, accepts, options = {}) ⇒ GLIOptionParser

Returns a new instance of GLIOptionParser.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gli/gli_option_parser.rb', line 13

def initialize(commands,flags,switches,accepts, options={})
  self.options = DEFAULT_OPTIONS.merge(options)

  command_finder       = CommandFinder.new(commands,
                                           :default_command => (options[:default_command] || :help),
                                           :autocomplete => options[:autocomplete])
  @global_option_parser = GlobalOptionParser.new(OptionParserFactory.new(flags,switches,accepts),command_finder,flags, :command_missing_block => options[:command_missing_block])
  @accepts              = accepts
  if options[:argument_handling_strategy] == :strict && options[:subcommand_option_handling_strategy] != :normal
    raise ArgumentError, "To use strict argument handling, you must enable normal subcommand_option_handling, e.g. subcommand_option_handling :normal"
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/gli/gli_option_parser.rb', line 4

def options
  @options
end

Instance Method Details

#parse_options(args) ⇒ Object

Given the command-line argument array, returns an OptionParsingResult



27
28
29
30
31
32
33
34
# File 'lib/gli/gli_option_parser.rb', line 27

def parse_options(args) # :nodoc:
  option_parser_class = self.class.const_get("#{options[:subcommand_option_handling_strategy].to_s.capitalize}CommandOptionParser")
  OptionParsingResult.new.tap { |parsing_result|
    parsing_result.arguments = args
    parsing_result = @global_option_parser.parse!(parsing_result)
    option_parser_class.new(@accepts).parse!(parsing_result, options[:argument_handling_strategy], options[:autocomplete])
  }
end