class Autorake::Application

Public Instance Methods

add_option(names, *desc_arg_call) click to toggle source
# File lib/autorake/application.rb, line 70
def add_option names, *desc_arg_call
  o = Option[ *desc_arg_call]
  names.each { |n| @options[ n] = o }
end
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
define_options() click to toggle source
# File lib/autorake/application.rb, line 65
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) { ||| $*.shift| ... } click to toggle source
# File lib/autorake/application.rb, line 91
def do_option a
  o = @options[ a]
  o or raise "Unknown option: #{a}"
  c = o.call
  if o.arg then
    g = yield || $*.shift
    c.push g
  end
  send *c
  @rest.delete o
end
environ(nam, val) click to toggle source
# File lib/autorake/application.rb, line 103
def environ nam, val
  raise "Define your own environment setter."
end
help() click to toggle source
# File lib/autorake/application.rb, line 107
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 75
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
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 end
            when /\A-/ then
              arg = $'
              until (a = arg.slice! 0, 1).empty? do
                do_option a do
                  arg.slice! 0, arg.length unless arg.empty?
                end
              end
            else
              n, v = arg.split "="
              environ n, v
          end
        end
      end
      execute
    rescue Done
    rescue
      raise if @verbose
      $stderr.puts "#$! (#{$!.class})"
      exit 1
    end

    private

    def define_options
      add_option %w(h help),    "display this help",           nil, :help
      add_option %w(V version), "display version information", nil, :version
    end

    def add_option names, *desc_arg_call
      o = Option[ *desc_arg_call]
      names.each { |n| @options[ n] = o }
    end

    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

    def do_option a
      o = @options[ a]
      o or raise "Unknown option: #{a}"
      c = o.call
      if o.arg then
        g = yield || $*.shift
        c.push g
      end
      send *c
      @rest.delete o
    end

    def environ nam, val
      raise "Define your own environment setter."
    end

    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

    def version
      require "autorake/version"
      puts <<-EOT
#{NAME} #{VERSION}  --  #{SUMMARY}

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

#{HOMEPAGE}
      EOT
      raise Done
    end

  end
version() click to toggle source
# File lib/autorake/application.rb, line 122
    def version
      require "autorake/version"
      puts <<-EOT
#{NAME} #{VERSION}  --  #{SUMMARY}

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

#{HOMEPAGE}
      EOT
      raise Done
    end