<commit_before>module Nanoc::CLI

  # TODO document
  class Base

    attr_reader :commands, :site

    # TODO document
    def initialize
      create_commands
    end

    # TODO document
    def run(args)
      # Check arguments
      if args.length == 0
        @help_command.run([], [])
        exit 1
      end

      # Find version or help options
      if args.length == 1
        # Parse arguments
        begin
          parsed_arguments = Nanoc::CLI::OptionParser.parse(args[0..1], global_option_definitions)
        rescue Nanoc::CLI::OptionParser::IllegalOptionError => e
          puts "illegal option -- #{e}"
          exit 1
        end

        # Handle version option
        if parsed_arguments[:options].has_key?(:version)
          puts "nanoc #{Nanoc::VERSION} (c) 2007-2008 <NAME>."
          exit 1
        # Handle help option
        elsif parsed_arguments[:options].has_key?(:help)
          show_help
          exit 1
        end
      end

      # Find command
      command = command_named(args[0])

      # Get extended option definitions (with help)
      extended_option_definitions = command.option_definitions + [
        {
          :long => 'help', :short => 'h', :argument => :forbidden,
          :desc => 'show this help message and quit'
        },
        # --verbose
        {
          :long => 'verbose', :short => 'V', :argument => :forbidden,
          :desc => 'enable more detailed output'
        }
      ]

      # Parse arguments
      begin
        parsed_arguments = Nanoc::CLI::OptionParser.parse(args[1..-1], extended_option_definitions)
      rescue Nanoc::CLI::OptionParser::IllegalOptionError => e
        puts "illegal option -- #{e}"
        exit 1
      rescue Nanoc::CLI::OptionParser::OptionRequiresAnArgumentError => e
        puts "option requires an argument -- #{e}"
        exit 1
      end

      # Check help option
      if parsed_arguments[:options].has_key?(:help)
        show_help(command)
        exit 1
      end

      # Check verbose option
      if parsed_arguments[:options].has_key?(:verbose)
        Nanoc::CLI::Logger.instance.level = :low
      end

      # Find and run command
      command.run(parsed_arguments[:options], parsed_arguments[:arguments])
    end

    # TODO document
    def command_named(name)
      # Find by exact name or alias
      command = @commands.find { |c| c.name == name or c.aliases.include?(name) }
      return command unless command.nil?

      # Find by approximation
      commands = @commands.select { |c| c.name[0, name.length] == name }
      if commands.length > 1
        puts "nanoc: '#{name}' is ambiguous:"
        puts "  #{commands.map { |c| c.name }.join(' ') }"
        exit 1
      elsif commands.length == 0
        puts "nanoc: unknown command '#{name}'\n"
        show_help
        exit 1
      else
        return commands[0]
      end
    end

    # TODO document
    def show_help(command=nil)
      if command.nil?
        @help_command.run([], [])
      else
        @help_command.run([], [ command.name ])
      end
    end

    # TODO document
    def require_site
      if site.nil?
        puts 'The current working directory does not seem to be a ' +
             'valid/complete nanoc site directory; aborting.'
        exit 1
      end
    end

    # TODO document
    def site
      # Load site if possible
      if File.file?('config.yaml') and @site.nil?
        begin
          @site = Nanoc::Site.new(YAML.load_file('config.yaml'))
          @site.load_data
        rescue Nanoc::Errors::UnknownDataSourceError => e
          puts "Unknown data source: #{e}"
          exit 1
        rescue Nanoc::Errors::UnknownRouterError => e
          puts "Unknown router: #{e}"
          exit 1
        end
      end

      @site
    end

    # TODO document
    def global_option_definitions
      [
        {
          :long => 'help', :short => 'h', :argument => :forbidden,
          :desc => 'show this help message and quit'
        },
        {
          :long => 'version', :short => 'v', :argument => :forbidden,
          :desc => 'show version information and quit'
        }
      ]
    end

  protected

    def create_commands
      @commands = []

      # Create specific commands
      @help_command = HelpCommand.new
      @commands << @help_command

      # Create general commands
      @commands << AutocompileCommand.new
      @commands << CompileCommand.new
      @commands << CreateLayoutCommand.new
      @commands << CreatePageCommand.new
      @commands << CreateSiteCommand.new
      @commands << CreateTemplateCommand.new
      @commands << InfoCommand.new
      @commands << SwitchCommand.new
      @commands << UpdateCommand.new

      # Set base
      @commands.each { |c| c.base = self }
    end

  end

end
<commit_msg>improved site loading error reporting<commit_after>module Nanoc::CLI

  # TODO document
  class Base

    attr_reader :commands, :site

    # TODO document
    def initialize
      create_commands
    end

    # TODO document
    def run(args)
      # Check arguments
      if args.length == 0
        @help_command.run([], [])
        exit 1
      end

      # Find version or help options
      if args.length == 1
        # Parse arguments
        begin
          parsed_arguments = Nanoc::CLI::OptionParser.parse(args[0..1], global_option_definitions)
        rescue Nanoc::CLI::OptionParser::IllegalOptionError => e
          puts "illegal option -- #{e}"
          exit 1
        end

        # Handle version option
        if parsed_arguments[:options].has_key?(:version)
          puts "nanoc #{Nanoc::VERSION} (c) 2007-2008 <NAME>."
          exit 1
        # Handle help option
        elsif parsed_arguments[:options].has_key?(:help)
          show_help
          exit 1
        end
      end

      # Find command
      command = command_named(args[0])

      # Get extended option definitions (with help)
      extended_option_definitions = command.option_definitions + [
        {
          :long => 'help', :short => 'h', :argument => :forbidden,
          :desc => 'show this help message and quit'
        },
        # --verbose
        {
          :long => 'verbose', :short => 'V', :argument => :forbidden,
          :desc => 'enable more detailed output'
        }
      ]

      # Parse arguments
      begin
        parsed_arguments = Nanoc::CLI::OptionParser.parse(args[1..-1], extended_option_definitions)
      rescue Nanoc::CLI::OptionParser::IllegalOptionError => e
        puts "illegal option -- #{e}"
        exit 1
      rescue Nanoc::CLI::OptionParser::OptionRequiresAnArgumentError => e
        puts "option requires an argument -- #{e}"
        exit 1
      end

      # Check help option
      if parsed_arguments[:options].has_key?(:help)
        show_help(command)
        exit 1
      end

      # Check verbose option
      if parsed_arguments[:options].has_key?(:verbose)
        Nanoc::CLI::Logger.instance.level = :low
      end

      # Find and run command
      command.run(parsed_arguments[:options], parsed_arguments[:arguments])
    end

    # TODO document
    def command_named(name)
      # Find by exact name or alias
      command = @commands.find { |c| c.name == name or c.aliases.include?(name) }
      return command unless command.nil?

      # Find by approximation
      commands = @commands.select { |c| c.name[0, name.length] == name }
      if commands.length > 1
        puts "nanoc: '#{name}' is ambiguous:"
        puts "  #{commands.map { |c| c.name }.join(' ') }"
        exit 1
      elsif commands.length == 0
        puts "nanoc: unknown command '#{name}'\n"
        show_help
        exit 1
      else
        return commands[0]
      end
    end

    # TODO document
    def show_help(command=nil)
      if command.nil?
        @help_command.run([], [])
      else
        @help_command.run([], [ command.name ])
      end
    end

    # TODO document
    def require_site
      if site.nil?
        puts 'The current working directory does not seem to be a ' +
             'valid/complete nanoc site directory; aborting.'
        exit 1
      end
    end

    # TODO document
    def site
      # Load site if possible
      if File.file?('config.yaml') and @site.nil?
        begin
          @site = Nanoc::Site.new(YAML.load_file('config.yaml'))
          @site.load_data
        rescue Nanoc::Errors::UnknownDataSourceError => e
          puts "Unknown data source: #{e}"
          exit 1
        rescue Nanoc::Errors::UnknownRouterError => e
          puts "Unknown router: #{e}"
          exit 1
        rescue Exception => e
          puts "ERROR: An exception occured while loading this site."
          puts
          puts "If you think this is a bug in nanoc, please do report it at"
          puts "<http://nanoc.stoneship.org/trac/newticket> -- thanks!"
          puts
          puts 'Message:'
          puts '  ' + e.message
          puts
          puts 'Backtrace:'
          puts e.backtrace.map { |t| '  - ' + t }.join("\n")
          exit 1
        end
      end

      @site
    end

    # TODO document
    def global_option_definitions
      [
        {
          :long => 'help', :short => 'h', :argument => :forbidden,
          :desc => 'show this help message and quit'
        },
        {
          :long => 'version', :short => 'v', :argument => :forbidden,
          :desc => 'show version information and quit'
        }
      ]
    end

  protected

    def create_commands
      @commands = []

      # Create specific commands
      @help_command = HelpCommand.new
      @commands << @help_command

      # Create general commands
      @commands << AutocompileCommand.new
      @commands << CompileCommand.new
      @commands << CreateLayoutCommand.new
      @commands << CreatePageCommand.new
      @commands << CreateSiteCommand.new
      @commands << CreateTemplateCommand.new
      @commands << InfoCommand.new
      @commands << SwitchCommand.new
      @commands << UpdateCommand.new

      # Set base
      @commands.each { |c| c.base = self }
    end

  end

end

{'fraction_non_alphanumeric': 0.10708614529575228, 'fraction_numerical': 0.006252481143310838, 'mean_word_length': 1.9482153306026917, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 51, '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': '2091761', 'n_tokens_mistral': 2922, 'n_tokens_neox': 2847, 'n_words': 794}