class Autorake::Application

Public Class Methods

attr_bang(*syms) click to toggle source
# File lib/autorake/application.rb, line 24
def attr_bang *syms
  syms.each { |sym|
    define_method :"#{sym}!" do
      instance_variable_set :"@#{sym}", true
    end
  }
  nil
end

Public Instance Methods

run() click to toggle source
# File lib/autorake/application.rb, line 35
def run
  process_options do
    while (arg = $*.shift) do
      case arg
        when /\A--/ then
          a, val = $'.split "=", 2
          do_option a do val || $*.shift end
        when /\A-/ then
          arg = $'
          until (a = arg.slice! 0, 1).empty? do
            do_option a do
              unless arg.empty? then
                arg.slice! 0, arg.length
              else
                $*.shift
              end
            end
          end
        else
          n, v = arg.split "="
          environ n, v||"1"
      end
    end
  end
  execute
rescue Done
rescue
  raise if @verbose
  $stderr.puts "#$! (#{$!.class})"
  exit 1
end

Private Instance Methods

add_option(names, *desc_arg_call) click to toggle source
# File lib/autorake/application.rb, line 74
def add_option names, *desc_arg_call
  o = Option[ *desc_arg_call]
  names.each { |n| @options[ n] = o }
end
define_options() click to toggle source
# File lib/autorake/application.rb, line 69
def define_options
  add_option %w(h help),    "display this help",           nil, :help
  add_option %w(V version), "display version information", nil, :version
end
do_option(a) { || ... } click to toggle source
# File lib/autorake/application.rb, line 95
def do_option a
  o = @options[ a]
  o or raise "Unknown option: #{a}"
  c = o.call
  if o.arg then
    c.push yield
  end
  send *c
  @rest.delete o
end
environ(nam, val) click to toggle source
# File lib/autorake/application.rb, line 106
def environ nam, val
  raise "Define your own environment setter."
end
help() click to toggle source
# File lib/autorake/application.rb, line 110
def help
  puts "  %-16s  %-16s  %-40s" % %w(Option Argument Description)
  prev = nil
  @options.each { |k,v|
    k = (k.length>1 ? "--" : "-") + k
    l = "  %-16s" % k
    unless v == prev then
      l << "  %-16s   %-40s" % [ v.arg, v.desc]
    end
    puts l
    prev = v
  }
  raise Done
end
process_options() { || ... } click to toggle source
# File lib/autorake/application.rb, line 79
def process_options
  @options = {}
  define_options
  @rest = @options.values.uniq
  yield
  @rest.each { |o|
    if o.arg then
      c = o.call
      c.push o.arg
      send *c
    end
  }
ensure
  @options = @rest = nil
end
version() click to toggle source
# File lib/autorake/application.rb, line 125
    def version
      require "autorake/version"
      puts <<~EOT
        #{NAME} #{VERSION}  --  #{SUMMARY}

        Copyright: #{COPYRIGHT}
        License:   #{LICENSE}

        #{HOMEPAGE}
      EOT
      raise Done
    end