<commit_before>require 'optparse'

module Bane
  class ArgumentsParser
    def initialize
      @service_maker = ServiceMaker.new
      @options = {host: default_host}
      @option_parser = init_option_parser
    end

    def parse(args)
      parse_options(args)

      return [] if (args.empty?)

      port = parse_port(args[0])
      services = args.drop(1)
      host = @options[:host]
      configuration = Configuration.new(port, host, services)
      create(configuration.services, configuration.port, configuration.host)
    end

    def usage
      @option_parser.help
    end

    private

    def init_option_parser
      OptionParser.new do |opts|
        opts.banner = 'Usage: bane [options] port [behaviors]'
        opts.separator ''
        opts.on('-l', '--listen-on-localhost',
          "Listen on localhost, (#{default_host}). [default]") do
          @options[:host] = default_host
        end
        opts.on('-a', '--listen-on-all-hosts', "Listen on all interfaces, (#{all_interfaces})") do
          @options[:host] = all_interfaces
        end
        opts.separator ''
        opts.separator 'All behaviors:'
        opts.separator @service_maker.all_service_names.map { |title| " - #{title}" }.join("\n")
      end
    end

    def parse_options(args)
      @option_parser.parse!(args)
      rescue OptionParser::InvalidOption => io
        raise ConfigurationError, io.message
    end

    def parse_port(port)
      Integer(port)
      rescue ArgumentError
        raise ConfigurationError, "Invalid port number: #{port}"
    end

    def create(behaviors, port, host)
      if behaviors.any?
        @service_maker.create(behaviors, port, host)
      else
        @service_maker.create_all(port, host)
      end
      rescue UnknownServiceError => ube
        raise ConfigurationError, ube.message
    end

    def all_interfaces
      Services::ALL_INTERFACES
    end

    def default_host
      Services::DEFAULT_HOST
    end

  end

  class ConfigurationError < RuntimeError; end

  Configuration = Struct.new(:port, :host, :services)
end
<commit_msg>Funny extraction which creates a class which seems to mirror what the parent ArgumentsParser class did.<commit_after>require 'optparse'

module Bane
  class ArgumentsParser
    def initialize
      @service_maker = ServiceMaker.new
      @options_parser = BaneOptionsParser.new
    end

    def parse(args)
      arguments = @options_parser.parse(args)

      return [] if arguments.empty? #TODO don't depend on mutated args

      create(arguments.services, arguments.port, arguments.host)
    end

    def usage
      @options_parser.usage
    end

    private

    def create(services, port, host)
      if services.any?
        @service_maker.create(services, port, host)
      else
        @service_maker.create_all(port, host)
      end
    rescue UnknownServiceError => ube
      raise ConfigurationError, ube.message
    end

  end

  class ConfigurationError < RuntimeError;
  end

  class BaneOptionsParser
    def initialize
      @service_maker = ServiceMaker.new
      @options = {host: default_host}
      @option_parser = init_option_parser
    end

    def parse(args)
      @option_parser.parse!(args)

      return ParsedArguments.empty if args.empty?

      port = parse_port(args[0])
      services = args.drop(1)
      ParsedArguments.new(port, @options[:host], services)
    rescue OptionParser::InvalidOption => io
      raise ConfigurationError, io.message
    end

    def usage
      @option_parser.help
    end

    private

    def init_option_parser
      OptionParser.new do |opts|
        opts.banner = 'Usage: bane [options] port [behaviors]'
        opts.separator ''
        opts.on('-l', '--listen-on-localhost',
                "Listen on localhost, (#{default_host}). [default]") do
          @options[:host] = default_host
        end
        opts.on('-a', '--listen-on-all-hosts', "Listen on all interfaces, (#{all_interfaces})") do
          @options[:host] = all_interfaces
        end
        opts.separator ''
        opts.separator 'All behaviors:'
        opts.separator @service_maker.all_service_names.map { |title| " - #{title}" }.join("\n")
      end
    end

    def parse_port(port)
      Integer(port)
    rescue ArgumentError
      raise ConfigurationError, "Invalid port number: #{port}"
    end

    def all_interfaces
      Services::ALL_INTERFACES
    end

    def default_host
      Services::DEFAULT_HOST
    end
  end

  class ParsedArguments

    attr_reader :port, :host, :services

    def initialize(port, host, services)
      @host = host
      @port = port
      @services = services
    end

    def empty?
      false
    end

    def self.empty
      EmptyArguments.new
    end
  end

  class EmptyArguments
    def empty?
      true
    end
  end

end

{'fraction_non_alphanumeric': 0.09378249116240382, 'fraction_numerical': 0.0008317737575379496, 'mean_word_length': 2.495639534883721, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6932162', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1378, 'n_words': 359}