An “option block” is a set of parseable options, starting from the beginning of the argument list, stopping with the first unknown command-line element. This class handles parsing that block
Parse the given argument list, returning the unparsed arguments and options
hash of parsed arguments. Exceptions from OptionParser
are
given to the handler configured in the constructor
argument list. This will be mutated
Returns unparsed args
# File lib/gli/gli_option_block_parser.rb, line 32 def parse!(args) do_parse(args) rescue OptionParser::InvalidOption => ex @exception_handler.call("Unknown option #{ex.args.join(' ')}",@extra_error_context) rescue OptionParser::InvalidArgument => ex @exception_handler.call("#{ex.reason}: #{ex.args.join(' ')}",@extra_error_context) end
# File lib/gli/gli_option_block_parser.rb, line 42 def do_parse(args) first_non_option = nil @option_parser_factory.option_parser.order!(args) do |non_option| first_non_option = non_option break end args.unshift(first_non_option) end
Create the parser using the given OptionParser
instance and
exception handling strategy.
An OptionParserFactory
instance, configured to parse wherever
you are on the command line
means of handling exceptions from OptionParser
. One of:
will be raised on errors with a message
will be called with a single argument - the error message.
# File lib/gli/gli_option_block_parser.rb, line 14 def initialize(option_parser_factory,exception_klass_or_block) @option_parser_factory = option_parser_factory @extra_error_context = nil @exception_handler = if exception_klass_or_block.kind_of?(Class) lambda { |message,extra_error_context| raise exception_klass_or_block,message } else exception_klass_or_block end end