<commit_before>module GLI
  # Parses the command-line options using an actual +OptionParser+
  class GLIOptionParser
    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
    end

    # Given the command-line argument array, returns an OptionParsingResult
    def parse_options(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

  private

    class GlobalOptionParser
      def initialize(option_parser_factory,command_finder,flags)
        @option_parser_factory = option_parser_factory
        @command_finder        = command_finder
        @flags                 = flags
      end

      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]
                         "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
      def verify_arguments!(arguments, command)
        # Go through all declared arguments for the command, counting the min and max number
        # of arguments
        min_nb_arguments = 0
        max_nb_arguments = 0
        command.arguments.each do |arg|
          if arg.optional?
            max_nb_arguments = max_nb_arguments + 1
          else
            min_nb_arguments = min_nb_arguments + 1
            max_nb_arguments = max_nb_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_nb_arguments = 99999
          end<commit_msg>Failsafe for complex scenarios<commit_after>module GLI
  # Parses the command-line options using an actual +OptionParser+
  class GLIOptionParser
    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
    end

    # Given the command-line argument array, returns an OptionParsingResult
    def parse_options(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

  private

    class GlobalOptionParser
      def initialize(option_parser_factory,command_finder,flags)
        @option_parser_factory = option_parser_factory
        @command_finder        = command_finder
        @flags                 = flags
      end

      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]
                         "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
      def verify_arguments!(arguments, command)
        # Lets assume that if the user sets a 'arg_name' for the command he has a complex scenario
        # and does not want us to validate his arguments
        return unless command.arguments_description.empty?

        # Go through all declared arguments for the command, counting the min and max number
        # of arguments
        min_nb_arguments = 0
        max_nb_arguments = 0
        command.arguments.each do |arg|
          if arg.optional?
            max_nb_arguments = max_nb_arguments + 1
          else
            min_nb_arguments = min_nb_arguments + 1
            max_nb_arguments = max_nb_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_nb_arguments = 99999
          end
{'fraction_non_alphanumeric': 0.06089264974282396, 'fraction_numerical': 0.003318400530944085, 'mean_word_length': 2.8151898734177214, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7530043', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1568, 'n_words': 331}