class Cielli

Constants

EXAMPLE_HELP
Version

Attributes

argv[RW]
env[RW]
help[RW]
root[RW]
source[RW]
stderr[RW]
stdin[RW]
stdout[RW]

Public Class Methods

after(&block) click to toggle source
# File lib/cielli.rb, line 64
def self.after(&block)
  @after ||= []
  @after << block if block
  @after
end
before(&block) click to toggle source
# File lib/cielli.rb, line 54
def self.before(&block)
  @before ||= []
  @before << block if block
  @before
end
dependencies() click to toggle source
# File lib/cielli/_lib.rb, line 13
def dependencies
  { }
end
help(*args) click to toggle source
# File lib/cielli.rb, line 167
def Cielli.help(*args)
  @help ||= nil

  unless args.empty?
    @help = utils.unindent(args.join)
  end

  @help
end
klass_for(&block) click to toggle source
# File lib/cielli.rb, line 198
def Cielli.klass_for(&block)
  Class.new(Cielli) do |klass|
    def klass.name; "Cielli::Klass__#{ SecureRandom.uuid.to_s.gsub('-', '_') }"; end
    klass.class_eval(&block)
  end
end
libdir(*args, &block) click to toggle source
# File lib/cielli/_lib.rb, line 30
def libdir(*args, &block)
  @libdir ||= File.dirname(File.expand_path(__FILE__).sub(/\.rb$/,''))
  args.empty? ? @libdir : File.join(@libdir, *args)
ensure
  if block
    begin
      $LOAD_PATH.unshift(@libdir)
      block.call()
    ensure
      $LOAD_PATH.shift()
    end
  end
end
load(*libs) click to toggle source
# File lib/cielli/_lib.rb, line 44
def load(*libs)
  libs = libs.join(' ').scan(/[^\s+]+/)
  libdir{ libs.each{|lib| Kernel.load(lib) } }
end
load_dependencies!() click to toggle source
# File lib/cielli/_lib.rb, line 17
def load_dependencies!
  begin 
    require 'rubygems'
  rescue LoadError
    nil
  end

  dependencies.each do |lib, dependency|
    gem(*dependency) if defined?(gem)
    require(lib)
  end
end
run(*args, &block) click to toggle source
# File lib/cielli.rb, line 177
def Cielli.run(*args, &block)
  modes =
    if args.empty?
      [nil]
    else
      args
    end

  modes.each do |mode|
    method_name =
      if mode
        "run_#{ mode }"
      else
        "run"
      end

    define_method(method_name, &block)
  end
end
run!(*args, &block) click to toggle source
# File lib/cielli.rb, line 205
def Cielli.run!(*args, &block)
  STDOUT.sync = true
  STDERR.sync = true

  %w[ PIPE INT ].each{|signal| Signal.trap(signal, "EXIT")}

  cielli = (
    source = 
      if binding.respond_to?(:source_location)
        File.expand_path(binding.source_location.first)
      else
        File.expand_path(eval('__FILE__', block.binding))
      end

    root = File.dirname(source)

    klass = Cielli.klass_for(&block)

    instance = klass.new

    instance.source = source

    instance.root = root

    instance
  )

  cielli.run!(*args)
end
summary() click to toggle source
# File lib/cielli/_lib.rb, line 9
def summary
  "a minimalist's toolkit for quickly writing well behaved CLI/cee-el-ahy programs"
end
u(&block) click to toggle source
# File lib/cielli.rb, line 243
def Cielli.u(&block)
  Cielli.utils(&block)
end
utils(&block) click to toggle source
# File lib/cielli.rb, line 239
def Cielli.utils(&block)
  block ? Cielli::Utils.module_eval(&block) : Cielli::Utils
end
version() click to toggle source
# File lib/cielli/_lib.rb, line 5
def version
  Version
end

Public Instance Methods

after() click to toggle source
# File lib/cielli.rb, line 70
def after
  self.class.after.each{|block| block.call}
end
before() click to toggle source
# File lib/cielli.rb, line 60
def before
  self.class.before.each{|block| block.call}
end
help!() click to toggle source
# File lib/cielli.rb, line 161
def help!
  run_help!
  abort
end
init!(env, argv) click to toggle source
# File lib/cielli.rb, line 44
def init!(env, argv)
  @klass = self.class
  @env = env.to_hash.dup
  @argv = argv.map{|arg| arg.dup}
  @stdout = $stdout.dup
  @stdin = $stdin.dup
  @stderr = $stderr.dup
  @help = @klass.help || utils.unindent(EXAMPLE_HELP)
end
parse_command_line!() click to toggle source
# File lib/cielli.rb, line 88
def parse_command_line!
  @options = Hash.new
  @opts = Hash.new

  argv = []
  head = []
  tail = []

  %w[ :: -- ].each do |stop|
    if((i = @argv.index(stop)))
      head = @argv.slice(0 ... i)
      tail = @argv.slice((i + 1) ... @argv.size) 
      @argv = head
      break
    end
  end

  @argv.each do |arg|
    case
      when arg =~ %r`^\s*:([^:\s]+)[=](.+)`
        key = $1
        val = $2
        @options[key] = val
      when arg =~ %r`^\s*(:+)(.+)`
        leader = $1
        key = $2
        val = leader.size.odd?
        @options[key] = val
      else
        argv.push(arg)
    end
  end

  argv += tail

  @argv.replace(argv)

  @options.each do |key, val|
    @opts[key.to_s.to_sym] = val
  end
  
  [@options, @opts]
end
run!(env = ENV, argv = ARGV) click to toggle source
# File lib/cielli.rb, line 35
def run!(env = ENV, argv = ARGV)
  before!
  init!(env, argv)
  parse_command_line!
  set_mode!
  run_mode!
  after!
end
run_help!() click to toggle source
# File lib/cielli.rb, line 157
def run_help!
  STDOUT.puts(@help)
end
run_mode!() click to toggle source
# File lib/cielli.rb, line 141
def run_mode!
  if @mode
    return send("run_#{ @mode }")
  else
    if respond_to?(:run)
      return send(:run)
    end

    if @argv.empty?
      run_help!
    else
      abort("#{ $0 } help")
    end
  end
end
set_mode!() click to toggle source
# File lib/cielli.rb, line 132
def set_mode!
  case
    when respond_to?("run_#{ @argv[0] }")
      @mode = @argv.shift
    else
      @mode = nil
  end
end