<commit_before># This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
# Copyright (c) 2010, <NAME>

require 'rubikon/application/base'
require 'rubikon/exceptions'
require 'rubikon/parameter'

module Rubikon

  # Instances of the Command class are used to define the real code that
  # should be executed when running the Application.
  class Command

    include Parameter

    attr_accessor :description
    attr_reader   :parameters

    # Create a new application command with the given name with a reference to
    # the app it belongs to
    #
    # +app+::   A reference to the Application instance this command belongs to
    # +name+::  The name of this command, used in Application options
    # +block+:: The code block which should be executed by this command
    def initialize(app, name, &block)
      super(name, nil)

      raise ArgumentError unless app.is_a? Application::Base

      @app              = app
      @long_parameters  = {}
      @parameters       = {}
      @short_parameters = {}

      if block_given?
        @block = block
      else
        @file_name = "#{@app.path}/commands/#{name}.rb"
        raise BlockMissingError unless File.exists?(@file_name)
        @block = Proc.new { @app.instance_eval(open(@file_name).read) }
      end
    end

    # Add a new Parameter for this command
    #
    # +parameter+:: The parameter to add to this command. This might also be a
    #               Hash where every key will be an alias to the corresponding
    #               value, e.g. <tt>{ :alias => :parameter }</tt>.
    def <<(parameter)
      if parameter.is_a? Hash
        parameter.each do |alias_name, name|
          if alias_name.size == 1
            @short_parameters[alias_name.to_sym] = name.to_sym
          else
            @long_parameters[alias_name.to_sym] = name.to_sym
          end
        end
      else
        raise ArgumentError unless parameter.is_a? Parameter
        @parameters[parameter.name] = parameter

        if parameter.name.to_s.size == 1
          @short_parameters[parameter.name] = parameter.name
        else
          @long_parameters[parameter.name] = parameter.name
        end
      end
    end

    # Parses the arguments of this command and sets each Parameter as active
    # if it has been supplied by the user on the command-line. Additional
    # arguments are passed to the individual parameters.
    #
    # +args+:: The arguments that have been passed to this command
    def parse_arguments(args)
      parameter = nil
      args.each do |arg|
        if arg.start_with?('--')
          parameter = @parameters[@long_parameters[arg[2..-1].to_sym]]
        elsif arg.start_with?('-')
          parameter = @parameters[@short_parameters[arg[1..-1].to_sym]]
        else
          parameter << arg
        end

        raise UnknownParameterError.new(arg) if parameter.nil?
        parameter.active!
      end

      @parameters.values.each do |param|
        if parameter.active? && parameter.args.size < parameter.arg_count
          raise MissingArgumentError.new(parameter.name)
        end<commit_msg>Fixed argument parsing in Command<commit_after># This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
# Copyright (c) 2010, <NAME>

require 'rubikon/application/base'
require 'rubikon/exceptions'
require 'rubikon/parameter'

module Rubikon

  # Instances of the Command class are used to define the real code that
  # should be executed when running the Application.
  class Command

    include Parameter

    attr_accessor :description
    attr_reader   :parameters

    # Create a new application command with the given name with a reference to
    # the app it belongs to
    #
    # +app+::   A reference to the Application instance this command belongs to
    # +name+::  The name of this command, used in Application options
    # +block+:: The code block which should be executed by this command
    def initialize(app, name, &block)
      super(name, nil)

      raise ArgumentError unless app.is_a? Application::Base

      @app              = app
      @long_parameters  = {}
      @parameters       = {}
      @short_parameters = {}

      if block_given?
        @block = block
      else
        @file_name = "#{@app.path}/commands/#{name}.rb"
        raise BlockMissingError unless File.exists?(@file_name)
        @block = Proc.new { @app.instance_eval(open(@file_name).read) }
      end
    end

    # Add a new Parameter for this command
    #
    # +parameter+:: The parameter to add to this command. This might also be a
    #               Hash where every key will be an alias to the corresponding
    #               value, e.g. <tt>{ :alias => :parameter }</tt>.
    def <<(parameter)
      if parameter.is_a? Hash
        parameter.each do |alias_name, name|
          if alias_name.size == 1
            @short_parameters[alias_name.to_sym] = name.to_sym
          else
            @long_parameters[alias_name.to_sym] = name.to_sym
          end
        end
      else
        raise ArgumentError unless parameter.is_a? Parameter
        @parameters[parameter.name] = parameter

        if parameter.name.to_s.size == 1
          @short_parameters[parameter.name] = parameter.name
        else
          @long_parameters[parameter.name] = parameter.name
        end
      end
    end

    # Parses the arguments of this command and sets each Parameter as active
    # if it has been supplied by the user on the command-line. Additional
    # arguments are passed to the individual parameters.
    #
    # +args+:: The arguments that have been passed to this command
    def parse_arguments(args)
      parameter = nil
      args.each do |arg|
        if arg.start_with?('--')
          parameter = @parameters[@long_parameters[arg[2..-1].to_sym]]
        elsif arg.start_with?('-')
          parameter = @parameters[@short_parameters[arg[1..-1].to_sym]]
        else
          parameter << arg
        end

        raise UnknownParameterError.new(arg) if parameter.nil?
        parameter.active!
      end

      @parameters.values.each do |param|
        if param.is_a?(Option) && param.active? && param.args.size < param.arg_count
          raise MissingArgumentError.new(param.name)
        end
{'fraction_non_alphanumeric': 0.08613345953620445, 'fraction_numerical': 0.003155071777882947, 'mean_word_length': 2.372340425531915, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, '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': '6777515', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1714, 'n_words': 592}