Class: GLI::GLIOptionParser
- Inherits:
-
Object
- Object
- GLI::GLIOptionParser
- 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
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(commands, flags, switches, accepts, options = {}) ⇒ GLIOptionParser
constructor
A new instance of GLIOptionParser.
-
#parse_options(args) ⇒ Object
Given the command-line argument array, returns an OptionParsingResult.
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, ={}) self. = DEFAULT_OPTIONS.merge() command_finder = CommandFinder.new(commands, :default_command => ([:default_command] || :help), :autocomplete => [:autocomplete]) @global_option_parser = GlobalOptionParser.new(OptionParserFactory.new(flags,switches,accepts),command_finder,flags, :command_missing_block => [:command_missing_block]) @accepts = accepts if [:argument_handling_strategy] == :strict && [: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
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/gli/gli_option_parser.rb', line 4 def @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 (args) # :nodoc: option_parser_class = self.class.const_get("#{[: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, [:argument_handling_strategy], [:autocomplete]) } end |