<commit_before>
require 'optparse'

module Commander
  class Runner
    
    #--
    # Exceptions
    #++

    class CommandError < StandardError; end
    class InvalidCommandError < CommandError; end
    
    attr_reader :commands, :options

    ##
    # Initialize a new command runner. Optionally
    # supplying +args+ for mocking, or arbitrary usage.
    
    def initialize args = ARGV
      @args, @commands, @aliases, @options = args, {}, {}, []
      @program = program_defaults
      create_default_commands
    end
    
    ##
    # Run command parsing and execution process.
    
    def run!
      require_program :name, :version, :description
      global_option '--help' do
        command(:help).run *@args[1..-1]
        return
      end
      global_option '--version' do 
        say "#{program(:name)} #{program(:version)}"
        return
      end
      parse_global_options
      call_active_command
    rescue InvalidCommandError
      say 'invalid command. Use --help for more information'
    rescue \
      OptionParser::InvalidOption, 
      OptionParser::InvalidArgument,
      OptionParser::MissingArgument => e
      say e
    end
    
    ##
    # Invoke the active command.
    
    def call_active_command<commit_msg>Refactored #run!<commit_after>
require 'optparse'

module Commander
  class Runner
    
    #--
    # Exceptions
    #++

    class CommandError < StandardError; end
    class InvalidCommandError < CommandError; end
    
    attr_reader :commands, :options

    ##
    # Initialize a new command runner. Optionally
    # supplying +args+ for mocking, or arbitrary usage.
    
    def initialize args = ARGV
      @args, @commands, @aliases, @options = args, {}, {}, []
      @program = program_defaults
      create_default_commands
    end
    
    ##
    # Run command parsing and execution process.
    
    def run!
      require_program :name, :version, :description
      global_option('-h', '--help') { command(:help).run *@args[1..-1]; return }
      global_option('-v', '--version') { say version; return } 
      parse_global_options
      call_active_command
    rescue InvalidCommandError
      say 'invalid command. Use --help for more information'
    rescue \
      OptionParser::InvalidOption, 
      OptionParser::InvalidArgument,
      OptionParser::MissingArgument => e
      say e
    end
    
    ##
    # Return program version.
    
    def version
      '%s %s' % [program(:name), program(:version)]
    end
    
    ##
    # Invoke the active command.
    
    def call_active_command
{'fraction_non_alphanumeric': 0.09613130128956623, 'fraction_numerical': 0.0015631105900742479, 'mean_word_length': 2.3551769331585843, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2190931', 'n_tokens_mistral': 724, 'n_tokens_neox': 712, 'n_words': 201}