<commit_before>require 'gli/command_line_token.rb'
require 'gli/command.rb'
require 'gli/switch.rb'
require 'gli/flag.rb'
require 'gli/options.rb'
require 'gli_version.rb'
require 'support/help.rb'
require 'support/rdoc.rb'
require 'support/initconfig.rb'
require 'etc'

# A means to define and parse a command line interface that works as
# Git's does, in that you specify global options, a command name, command
# specific options, and then command arguments.
module GLI
  extend self

  @@program_name = $0.split(/\//)[-1]
  @@post_block = nil
  @@pre_block = nil
  @@error_block = nil
  @@config_file = nil

  # Reset the GLI module internal data structures; mostly for testing
  def reset
    switches.clear
    flags.clear
    commands.clear
    @@config_file = nil
    clear_nexts
  end

  # describe the next switch, flag, or command.  This should be a
  # short, one-line description
  def desc(description); @@next_desc = description; end

  # Provide a longer, more detailed description.  This
  # will be reformatted and wrapped to fit in 80 columns
  def long_desc(long_desc); @@next_long_desc = long_desc; end

  # describe the argument name of the next flag
  def arg_name(name); @@next_arg_name = name; end

  # set the default value of the next flag
  def default_value(val); @@next_default_value = val; end

  # Create a flag, which is a switch that takes an argument
  def flag(*names)
    names = [names].flatten
    verify_unused(names,flags,switches,"in global options")
    flag = Flag.new(names,@@next_desc,@@next_arg_name,@@next_default_value,@@next_long_desc)
    flags[flag.name] = flag
    clear_nexts
  end

  # Create a switch
  def switch(*names)
    names = [names].flatten
    verify_unused(names,flags,switches,"in global options")
    switch = Switch.new(names,@@next_desc,@@next_long_desc)
    switches[switch.name] = switch
    clear_nexts
  end

  # Sets the config file.  If not an absolute path
  # sets the path to the user's home directory
  def config_file(filename)
    if filename =~ /^\//
      @@config_file = filename
    else
      @@config_file = Etc.getpwuid.dir + '/' + filename
    end
    commands[:initconfig] = InitConfig.new(@@config_file)
    @@config_file
  end

  # Define a command.
  def command(*names)
    command = Command.new([names].flatten,@@next_desc,@@next_arg_name,@@next_long_desc)
    commands[command.name] = command
    yield command
    clear_nexts
  end

  # Define a block to run after command line arguments are parsed
  # but before any command is run.  If this block raises an exception
  # the command specified will not be executed.
  # The block will receive the global-options,command,options, and arguments
  # If this block evaluates to true, the program will proceed; otherwise
  # the program will end immediately
  def pre(&a_proc)
    @@pre_block = a_proc
  end

  # Define a block to run after command hase been executed, only
  # if there was not an error.
  # The block will receive the global-options,command,options, and arguments
  def post(&a_proc)
    @@post_block = a_proc
  end

  # Define a block to run if an error occurs.
  # The block will receive any Exception that was caught.
  # It should return false to avoid the built-in error handling (which basically just
  # prints out a message)
  def on_error(&a_proc)
    @@error_block = a_proc
  end

  # Runs whatever command is needed based on the arguments.
  def run(args)
    rdoc = RDocCommand.new
    commands[:rdoc] = rdoc if !commands[:rdoc]
    commands[:help] = DefaultHelpCommand.new(rdoc) if !commands[:help]
    begin
      config = parse_config
      global_options,command,options,arguments = parse_options(args,config)
      copy_options_to_aliased_versions(global_options,command,options)
      proceed = true
      proceed = @@pre_block.call(global_options,command,options,arguments) if @@pre_block 
      if proceed
        command = commands[:help] if !command
        command.execute(global_options,options,arguments)
        @@post_block.call(global_options,command,options,arguments) if @@post_block 
      end
    rescue Exception => ex
      regular_error_handling = true
      regular_error_handling = @@error_block.call(ex) if @@error_block

      if regular_error_handling
        puts "error: #{ex.message}"
      end
    end
  end

  # Copies all options in both global_options and options to keys for the aliases of those flags.
  # For example, if a flag works with either -f or --flag, this will copy the value from [:f] to [:flag]
  # to allow the user to access the options by any alias
  def copy_options_to_aliased_versions(global_options,command,options)
    copy_options_to_aliases(global_options,self)
    copy_options_to_aliases(options,command)
  end

  # For each option in options, copies its value to keys for the aliases of the flags or
  # switches in gli_like
  #
  # options - Hash of options parsed from command line; this is an I/O param
  # gli_like - Object resonding to flags and switches in the same way that GLI or a Command instance do
  def copy_options_to_aliases(options,gli_like)
    options.each do |key,value|
      if gli_like.flags[key] && gli_like.flags[key].aliases
        gli_like.flags[key].aliases.each do |alias_name|
          options[alias_name] = value
        end
      elsif gli_like.switches[key] && gli_like.switches[key].aliases
        gli_like.switches[key].aliases.each do |alias_name|
          options[alias_name] = value
        end
      end
    end<commit_msg>In 1.9.2 you can't mod a hash you are iterating over<commit_after>require 'gli/command_line_token.rb'
require 'gli/command.rb'
require 'gli/switch.rb'
require 'gli/flag.rb'
require 'gli/options.rb'
require 'gli_version.rb'
require 'support/help.rb'
require 'support/rdoc.rb'
require 'support/initconfig.rb'
require 'etc'

# A means to define and parse a command line interface that works as
# Git's does, in that you specify global options, a command name, command
# specific options, and then command arguments.
module GLI
  extend self

  @@program_name = $0.split(/\//)[-1]
  @@post_block = nil
  @@pre_block = nil
  @@error_block = nil
  @@config_file = nil

  # Reset the GLI module internal data structures; mostly for testing
  def reset
    switches.clear
    flags.clear
    commands.clear
    @@config_file = nil
    clear_nexts
  end

  # describe the next switch, flag, or command.  This should be a
  # short, one-line description
  def desc(description); @@next_desc = description; end

  # Provide a longer, more detailed description.  This
  # will be reformatted and wrapped to fit in 80 columns
  def long_desc(long_desc); @@next_long_desc = long_desc; end

  # describe the argument name of the next flag
  def arg_name(name); @@next_arg_name = name; end

  # set the default value of the next flag
  def default_value(val); @@next_default_value = val; end

  # Create a flag, which is a switch that takes an argument
  def flag(*names)
    names = [names].flatten
    verify_unused(names,flags,switches,"in global options")
    flag = Flag.new(names,@@next_desc,@@next_arg_name,@@next_default_value,@@next_long_desc)
    flags[flag.name] = flag
    clear_nexts
  end

  # Create a switch
  def switch(*names)
    names = [names].flatten
    verify_unused(names,flags,switches,"in global options")
    switch = Switch.new(names,@@next_desc,@@next_long_desc)
    switches[switch.name] = switch
    clear_nexts
  end

  # Sets the config file.  If not an absolute path
  # sets the path to the user's home directory
  def config_file(filename)
    if filename =~ /^\//
      @@config_file = filename
    else
      @@config_file = Etc.getpwuid.dir + '/' + filename
    end
    commands[:initconfig] = InitConfig.new(@@config_file)
    @@config_file
  end

  # Define a command.
  def command(*names)
    command = Command.new([names].flatten,@@next_desc,@@next_arg_name,@@next_long_desc)
    commands[command.name] = command
    yield command
    clear_nexts
  end

  # Define a block to run after command line arguments are parsed
  # but before any command is run.  If this block raises an exception
  # the command specified will not be executed.
  # The block will receive the global-options,command,options, and arguments
  # If this block evaluates to true, the program will proceed; otherwise
  # the program will end immediately
  def pre(&a_proc)
    @@pre_block = a_proc
  end

  # Define a block to run after command hase been executed, only
  # if there was not an error.
  # The block will receive the global-options,command,options, and arguments
  def post(&a_proc)
    @@post_block = a_proc
  end

  # Define a block to run if an error occurs.
  # The block will receive any Exception that was caught.
  # It should return false to avoid the built-in error handling (which basically just
  # prints out a message)
  def on_error(&a_proc)
    @@error_block = a_proc
  end

  # Runs whatever command is needed based on the arguments.
  def run(args)
    rdoc = RDocCommand.new
    commands[:rdoc] = rdoc if !commands[:rdoc]
    commands[:help] = DefaultHelpCommand.new(rdoc) if !commands[:help]
    begin
      config = parse_config
      global_options,command,options,arguments = parse_options(args,config)
      copy_options_to_aliased_versions(global_options,command,options)
      proceed = true
      proceed = @@pre_block.call(global_options,command,options,arguments) if @@pre_block 
      if proceed
        command = commands[:help] if !command
        command.execute(global_options,options,arguments)
        @@post_block.call(global_options,command,options,arguments) if @@post_block 
      end
    rescue Exception => ex
      regular_error_handling = true
      regular_error_handling = @@error_block.call(ex) if @@error_block

      if regular_error_handling
        puts "error: #{ex.message}"
      end
    end
  end

  # Copies all options in both global_options and options to keys for the aliases of those flags.
  # For example, if a flag works with either -f or --flag, this will copy the value from [:f] to [:flag]
  # to allow the user to access the options by any alias
  def copy_options_to_aliased_versions(global_options,command,options)
    copy_options_to_aliases(global_options,self)
    copy_options_to_aliases(options,command)
  end

  # For each option in options, copies its value to keys for the aliases of the flags or
  # switches in gli_like
  #
  # options - Hash of options parsed from command line; this is an I/O param
  # gli_like - Object resonding to flags and switches in the same way that GLI or a Command instance do
  def copy_options_to_aliases(options,gli_like)
    new_options = {}
    options.each do |key,value|
      if gli_like.flags[key] && gli_like.flags[key].aliases
        gli_like.flags[key].aliases.each do |alias_name|
          new_options[alias_name] = value
        end
      elsif gli_like.switches[key] && gli_like.switches[key].aliases
        gli_like.switches[key].aliases.each do |alias_name|
          new_options[alias_name] = value
        end
      end
    end
    options.merge!(new_options)
{'fraction_non_alphanumeric': 0.08519452449567723, 'fraction_numerical': 0.0009906340057636888, 'mean_word_length': 3.5662006578947367, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3244972', 'n_tokens_mistral': 3442, 'n_tokens_neox': 3353, 'n_words': 1119}