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
Instance Method Summary collapse
-
#initialize(commands, flags, switches, accepts, default_command = nil, subcommand_option_handling_strategy = :legacy, argument_handling_strategy = :loose) ⇒ 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, default_command = nil, subcommand_option_handling_strategy = :legacy, argument_handling_strategy = :loose) ⇒ GLIOptionParser
Returns a new instance of GLIOptionParser.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/gli/gli_option_parser.rb', line 4 def initialize(commands,flags,switches,accepts,default_command = nil,subcommand_option_handling_strategy=:legacy,argument_handling_strategy=:loose) command_finder = CommandFinder.new(commands,default_command || "help") @global_option_parser = GlobalOptionParser.new(OptionParserFactory.new(flags,switches,accepts),command_finder,flags) @accepts = accepts @subcommand_option_handling_strategy = subcommand_option_handling_strategy @argument_handling_strategy = argument_handling_strategy 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 Method Details
#parse_options(args) ⇒ Object
Given the command-line argument array, returns an OptionParsingResult
16 17 18 19 20 21 22 23 |
# File 'lib/gli/gli_option_parser.rb', line 16 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) } end |