Sha256: 6067f4af3cb59f15c9918299152a60139aa5d5a4917a62c6f4f10344622c5a41

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

# encoding: utf-8

require 'liquid/boot'
require 'mixlib/cli'

class CLI
  include Mixlib::CLI

  option :name,
    :short => '-n NAME',
    :long => '--name NAME',
    :description => 'Process name',
    :default => File.basename($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(debug: value, 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

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

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

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

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
liquid-ext-3.5.0 lib/liquid/cli.rb
liquid-ext-3.4.2 lib/liquid/cli.rb