<commit_before># encoding: utf-8

require 'madvertise/boot'
require 'mixlib/cli'

$0 = File.basename($0)

class CLI
  include Mixlib::CLI

  option :configfile,
    short: '-c FILE',
    long: '--config FILE',
    description: 'Configuration File to load'

  option :name,
    :short => '-n NAME',
    :long => '--name NAME',
    :description => 'Process name',
    :default => $0,
    :proc => ->(value) { $0 = value }

  option :environment,
    :short => '-e ENVIRONMENT',
    :long => '--environment ENVIRONMENT',
    :description => "Set the daemon environment",
    :default => "development",
    :proc => ->(value) { Env.set(value) }

  option :debug,
    :short => '-D',
    :long => '--debug',
    :description => "Enable debug output",
    :boolean => true,
    :default => false,
    :proc => ->(value) { $conf.mixin(log_level: value ? :debug : :info); value }

  option :help,
    :short => '-h',
    :long => '--help',
    :description => "Show this message",
    :on => :tail,
    :boolean => true,
    :show_options => true,
    :exit => 0

  def option(name, args)
    args[:on] ||= :on
    args[:boolean] ||= false
    args[:required] ||= false
    args[:proc] ||= nil
    args[:show_options] ||= false
    args[:exit] ||= nil

    if args.has_key?(:default)
      config[name.to_sym] = args[:default]
    end

    options[name.to_sym] = args
  end

  def self.for(cls, &block)
    cli = new
    cli.instance_eval(&block) if block_given?
    cli.parse_options

    $log.info("cli:initialize", cli.config)
    $conf.reload!

    opts = cli.config.merge({
      interval: 1,
      logger: $log,
    })

    cls.new(opts)
  end

end<commit_msg>cli: monkey patch opts into cli class<commit_after># encoding: utf-8

require 'madvertise/boot'
require 'mixlib/cli'

$0 = File.basename($0)

class CLI
  include Mixlib::CLI

  option :configfile,
    short: '-c FILE',
    long: '--config FILE',
    description: 'Configuration File to load'

  option :name,
    :short => '-n NAME',
    :long => '--name NAME',
    :description => 'Process name',
    :default => $0,
    :proc => ->(value) { $0 = value }

  option :environment,
    :short => '-e ENVIRONMENT',
    :long => '--environment ENVIRONMENT',
    :description => "Set the daemon environment",
    :default => "development",
    :proc => ->(value) { Env.set(value) }

  option :debug,
    :short => '-D',
    :long => '--debug',
    :description => "Enable debug output",
    :boolean => true,
    :default => false,
    :proc => ->(value) { $conf.mixin(log_level: value ? :debug : :info); value }

  option :help,
    :short => '-h',
    :long => '--help',
    :description => "Show this message",
    :on => :tail,
    :boolean => true,
    :show_options => true,
    :exit => 0

  def option(name, args)
    args[:on] ||= :on
    args[:boolean] ||= false
    args[:required] ||= false
    args[:proc] ||= nil
    args[:show_options] ||= false
    args[:exit] ||= nil

    if args.has_key?(:default)
      config[name.to_sym] = args[:default]
    end

    options[name.to_sym] = args
  end

  def self.for(cls, &block)
    cli = new
    cli.instance_eval(&block) if block_given?
    cli.parse_options

    $log.info("cli:initialize", cli.config)
    $conf.reload!

    # infer some variables
    opts = cli.config.merge({
      fqdn: Socket.gethostbyname(Socket.gethostname).first
    })

    # sneak in opts without subclassing
    cls.allocate.tap do |obj|
      obj.define_singleton_method(:opts) { opts }
      obj.send(:initialize)
    end
  end

end
{'fraction_non_alphanumeric': 0.17446808510638298, 'fraction_numerical': 0.0036879432624113474, 'mean_word_length': 2.7312169312169314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, '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': '6235107', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1206, 'n_words': 305}