<commit_before>require 'optparse'
require 'belafonte/errors'
require 'belafonte/help'
require 'belafonte/parser'
require 'belafonte/helpers'

module Belafonte
  module Rhythm
    def execute!
      (@parser = Parser.new(
        switches: configured_switches,
        options: configured_options,
        commands: configured_subcommands,
        arguments: configured_args,
        argv: @argv
      )).parsed.tap do |parsed|
        @switches = parsed[:switches]
        @options = parsed[:options]
        @arguments = parsed[:args]
        activate_help! if parsed[:help]
      end

      @command = arg(:command).shift if arg(:command)

      run_setup

      unless dispatch || show_help || run_handle
        stderr.puts "No handler for the provided command line"
        return 1
      end

      0
    end

    private
    def parent
      @parent
    end

    def subcommand_instance(command)
      command_class = configured_subcommands.
        find {|subcommand| subcommand.info(:title) == command}

      return nil unless command_class

      command_class.new(arg(:command), stdin, stdout, stderr, kernel, self)
    end

    def dispatch
      return false if @command.nil?
      handler = subcommand_instance(@command)

      unless handler
        activate_help!
        return false
      end<commit_msg>Belafonte catches otherwise uncaught exceptions<commit_after>require 'optparse'
require 'belafonte/errors'
require 'belafonte/help'
require 'belafonte/parser'
require 'belafonte/helpers'
require 'belafonte/wrapomatic'

module Belafonte
  module Rhythm
    def execute!
      (@parser = Parser.new(
        switches: configured_switches,
        options: configured_options,
        commands: configured_subcommands,
        arguments: configured_args,
        argv: @argv
      )).parsed.tap do |parsed|
        @switches = parsed[:switches]
        @options = parsed[:options]
        @arguments = parsed[:args]
        activate_help! if parsed[:help]
      end

      @command = arg(:command).shift if arg(:command)

      begin
        run_setup

        unless dispatch || show_help || run_handle
          stderr.puts "No handler for the provided command line"
          return 1
        end
      rescue => uncaught_error
        stderr.puts "The application encountered the following error:"
        stderr.puts Wrapomatic.wrap(uncaught_error.to_s, 1)
        return 255
      end

      0
    end

    private
    def parent
      @parent
    end

    def subcommand_instance(command)
      command_class = configured_subcommands.
        find {|subcommand| subcommand.info(:title) == command}

      return nil unless command_class

      command_class.new(arg(:command), stdin, stdout, stderr, kernel, self)
    end

    def dispatch
      return false if @command.nil?
      handler = subcommand_instance(@command)

      unless handler
        activate_help!
        return false
      end
{'fraction_non_alphanumeric': 0.07900136798905609, 'fraction_numerical': 0.0027359781121751026, 'mean_word_length': 2.51140456182473, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5413148', 'n_tokens_mistral': 830, 'n_tokens_neox': 799, 'n_words': 223}