class Application

appl.rb – Application class

Constants

ALIASES
APPL_VERSION
OPTIONS_ENV
STOPOPT
UNKNOWN
UNPROCA
W_ARGS
W_OPTS

Public Class Methods

alias_option(orig, opt) click to toggle source
# File lib/appl.rb, line 146
def alias_option orig, opt
  unalias_option opt
  @aliases[ opt.to_s] = orig.to_s
  nil
end
all_names() click to toggle source
# File lib/appl.rb, line 220
def all_names
  [ self::NAME, *self::ALIASES].join "|"
end
cmdline_arguments() click to toggle source
# File lib/appl.rb, line 284
def cmdline_arguments
  r = []
  oe = self::OPTIONS_ENV
  eo = ENV[ oe] if oe
  if eo then
    eo.scan /"((?:\\.|[^"])*")|[^" \t]+/ do
      r.push $1 ? (eval $1) : $&
    end
  end
  r.concat $*
  $*.clear
  r
end
def_option(opt, *param)
Alias for: define_option
define_option(opt, *param) click to toggle source
# File lib/appl.rb, line 124
def define_option opt, *param
  delete_option opt
  act = param.shift
  desc = param.pop
  arg = param.shift
  if arg then
    if param.empty? then
      arg, dfl = arg.split /:/, 2
      if dfl =~ /\A:/ then
        dfl = $'.to_sym
      end
    else
      dfl = param.shift
    end
  end
  d = param.map { |x| "#{x}#$/" }.join
  desc.insert 0, d
  @options[ opt.to_s] = [ desc, arg, dfl, act]
  nil
end
Also aliased as: def_option
delete_option(opt) click to toggle source
# File lib/appl.rb, line 152
def delete_option opt
  @aliases.reject! { |k,v| v == opt }
  @options.delete opt
  nil
end
each_option() { |opt, desc, arg, dfl, act| ... } click to toggle source
# File lib/appl.rb, line 185
def each_option
  @options.each { |opt,(desc,arg,dfl,act)|
    case dfl
      when Symbol then dfl = const_get dfl
    end
    yield opt, desc, arg, dfl, act
  }
end
full_name() click to toggle source
# File lib/appl.rb, line 224
def full_name
  n = []
  s = self
  loop do
    l = s.all_names
    n.unshift l
    break if s == root
    s = s.root
  end
  n.join ' '
end
help() { || ... } click to toggle source
# File lib/appl.rb, line 236
def help
  fn = full_name
  puts "#{fn}  --  #{self::SUMMARY}"
  puts
  d = self::DESCRIPTION.gsub /#(\w+?)?#/ do
    case $1
      when "NAME" then fn
    end
  end
  puts d
  puts
  show_options
  if block_given? then
    puts
    yield
  end
end
new(name = nil, args = nil) click to toggle source
# File lib/appl.rb, line 24
def initialize name = nil, args = nil
  @name, @args = name, args
  self.class.each_option { |opt,desc,arg,dfl,act|
    begin
      send act, dfl if dfl
    rescue NoMethodError
      raise OptionError, "Option action `#{act}' is not defined."
    end
  }
  while @args.first =~ /\A-/ do
    opt = $'
    @args.shift
    if opt =~ /\A-/ then
      break if !$' or $'.empty?
      opt = $'
      if opt =~ /^=/ then opt = $` ; @args.unshift $' end
      act = self.class.option_act @args, opt, nil
      send *act
    else
      until not opt or opt.empty? do
        c = opt.slice! 0, 1
        if opt =~ /^=/ then opt = nil ; @args.unshift $' end
        act = self.class.option_act @args, c, opt
        send *act
      end
    end
  end
end
option_act(args, opt, rest) click to toggle source
# File lib/appl.rb, line 194
def option_act args, opt, rest
  dada = find_option_act opt
  dada or raise OptionError, root::UNKNOWN % opt
  desc, arg, dfl, act = *dada
  r = [ act]
  if arg then
    p = rest.slice! 0, rest.length if rest and not rest.empty?
    r.push p||args.shift
  end
  r
end
root() click to toggle source
# File lib/appl.rb, line 216
def root
  self
end
run(args = nil) click to toggle source
# File lib/appl.rb, line 89
def run args = nil
  e = execute args
  exit e
end
show_message(msg, extra = nil) click to toggle source
# File lib/appl.rb, line 271
def show_message msg, extra = nil
  if $stderr.tty? then
    msg = "\e[31;1m#{msg}\e[m"
    if extra then
      extra = "\e[31m#{extra}\e[m"
    end
  end
  if extra then
    msg = [ msg, extra].join " "
  end
  $stderr.puts msg
end
show_options() click to toggle source
# File lib/appl.rb, line 206
def show_options
  options_desc do |opt,arg,dfl,desc|
    opt = opt.length == 1 ? "-#{opt}" : "--#{opt}"
    arg &&= "#{arg}"
    dfl &&= "[#{dfl}]"
    arg << dfl if arg && dfl
    puts "  %-*s  %-*s  %s" % [ root::W_OPTS, opt, root::W_ARGS, arg, desc]
  end
end
unalias_option(opt) click to toggle source
# File lib/appl.rb, line 158
def unalias_option opt
  @aliases.delete opt
  nil
end
version() click to toggle source
# File lib/appl.rb, line 254
def version
  puts "#{self::NAME} #{self::VERSION}  --  #{self::SUMMARY}"
  puts self::COPYRIGHT if const_defined? :COPYRIGHT
  puts "License: #{self::LICENSE}" if const_defined? :LICENSE
  a = []
  a.push   self::AUTHOR  if const_defined? :AUTHOR
  a.concat self::AUTHORS if const_defined? :AUTHORS
  if a.any? then
    a.flatten!
    a.uniq!
    j = a.join ", "
    h = a.length == 1 ? "Author" : "Authors"
    puts "#{h}: #{j}"
  end
  puts "Ruby version: #{RUBY_VERSION}"
end

Protected Class Methods

find_option_act(opt) click to toggle source
# File lib/appl.rb, line 165
def find_option_act opt
  @options[ opt] || @options[ @aliases[ opt]]
end
options_desc() { |opt, arg, dfl, desc| ... } click to toggle source
# File lib/appl.rb, line 169
def options_desc &block
  a = Hash.new do |h,k| h[ k] = [] end
  @aliases.each { |k,v|
    a[ v].push k
  }
  each_option { |opt,desc,arg,dfl,|
    yield opt, arg, dfl, desc
    a[ opt].each { |l|
      yield l, nil, nil, nil
    }
  }
  yield "", nil, nil, root::STOPOPT
end

Private Class Methods

attr_bang(*syms) click to toggle source
# File lib/appl.rb, line 113
def attr_bang *syms
  syms.each { |sym|
    define_method :"#{sym}!" do
      instance_variable_set :"@#{sym}", true
    end
  }
  nil
end
execute(args = nil) click to toggle source
# File lib/appl.rb, line 96
def execute args = nil
  n = File.basename $0
  i = new n, args||cmdline_arguments
  i.execute
rescue Done
  0
rescue OptionError
  show_message $!
  127
end
inherited(sub) click to toggle source
# File lib/appl.rb, line 107
def inherited sub
  sub.name or return
  o, a = @options.dup.to_h, @aliases.dup.to_h
  sub.instance_eval { @options, @aliases = o, a }
end

Public Instance Methods

execute() click to toggle source
# File lib/appl.rb, line 72
def execute
  r = run
  r = 0 unless Integer === r
  warn_unprocessed
  r
rescue SignalException
  raise if @debug
  self.class.show_message $!.inspect
  128 + $!.signo
rescue
  raise if @debug
  self.class.show_message "Error: #$!", "(#{$!.class})"
  $!.to_i rescue 1
end
help() click to toggle source
# File lib/appl.rb, line 53
def help
  self.class.help
  raise Done
end
run() click to toggle source
# File lib/appl.rb, line 63
def run
end
version() click to toggle source
# File lib/appl.rb, line 58
def version
  self.class.version
  raise Done
end
warn_unprocessed() click to toggle source
# File lib/appl.rb, line 66
def warn_unprocessed
  if @args.any? then
    $stderr.puts self.class.root::UNPROCA % (@args.join " ")
  end
end