class GLI::GLIOptionParser::GlobalOptionParser
Public Class Methods
new(option_parser_factory,command_finder,flags)
click to toggle source
# File lib/gli/gli_option_parser.rb, line 39 def initialize(option_parser_factory,command_finder,flags) @option_parser_factory = option_parser_factory @command_finder = command_finder @flags = flags end
Public Instance Methods
parse!(parsing_result)
click to toggle source
# File lib/gli/gli_option_parser.rb, line 45 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 = @command_finder.find_command(command_name) unless command_name == 'help' verify_required_options!(@flags, parsing_result.command, parsing_result.global_options) end parsing_result end
Protected Instance Methods
verify_arguments!(arguments, command)
click to toggle source
# File lib/gli/gli_option_parser.rb, line 61 def verify_arguments!(arguments, command) # Lets assume that if the user sets a 'arg_name' for the command it is for a complex scenario # and we should not validate the arguments return unless command.arguments_description.empty? # Go through all declared arguments for the command, counting the min and max number # of arguments min_number_of_arguments = 0 max_number_of_arguments = 0 command.arguments.each do |arg| if arg.optional? max_number_of_arguments = max_number_of_arguments + 1 else min_number_of_arguments = min_number_of_arguments + 1 max_number_of_arguments = max_number_of_arguments + 1 end # Special case, as soon as we have a 'multiple' arguments, all bets are off for the # maximum number of arguments ! if arg.multiple? max_number_of_arguments = 99999 end end # Now validate the number of arguments if arguments.size < min_number_of_arguments raise MissingRequiredArgumentsException.new("Not enough arguments for command", command) end if arguments.size > max_number_of_arguments raise MissingRequiredArgumentsException.new("Too many arguments for command", command) end end
verify_required_options!(flags, command, options)
click to toggle source
# File lib/gli/gli_option_parser.rb, line 94 def verify_required_options!(flags, command, options) missing_required_options = flags.values. select(&:required?). reject { |option| options[option.name] != nil } unless missing_required_options.empty? missing_required_options.sort! raise MissingRequiredArgumentsException.new(missing_required_options.map { |option| "#{option.name} is required" }.join(', '), command) end end