class Application
appl.rb – Application class
Constants
- APPL_VERSION
- DESCRIPTION
- HASH_ORDER
- NAME
- OPTIONS_ENV
- STOPOPT
- SUMMARY
- UNKNOWN
- UNPROCA
- VERSION
Public Class Methods
new(args = nil)
click to toggle source
# File lib/appl.rb, line 19 def initialize args = nil @args = args||self.class.cmdline_arguments 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 $'.empty? opt = $' if opt =~ /=/ then opt = $` ; @args.unshift $' end act = self.class.option_act @args, opt, nil send *act else until opt.empty? do c = opt.slice! 0, 1 act = self.class.option_act @args, c, opt send *act end end end end
Public Instance Methods
alias_option(orig, opt)
click to toggle source
# File lib/appl.rb, line 159 def alias_option orig, opt unalias_option opt @aliases[ opt.to_s] = orig.to_s nil end
cmdline_arguments()
click to toggle source
# File lib/appl.rb, line 285 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 $* r end
define_option(opt, *param)
click to toggle source
# File lib/appl.rb, line 137 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
delete_option(opt)
click to toggle source
# File lib/appl.rb, line 165 def delete_option opt self < Application or return superclass.delete_option opt @options.delete opt @aliases.reject! { |k,v| v == opt } nil end
each_option() { |opt, desc, arg, dfl, act| ... }
click to toggle source
# File lib/appl.rb, line 223 def each_option o = all_options o = o.sort_by { |k,v| k.swapcase } unless HASH_ORDER o.each { |opt,(desc,arg,dfl,act)| case dfl when Symbol then dfl = const_get dfl end yield opt, desc, arg, dfl, act } end
execute()
click to toggle source
# File lib/appl.rb, line 76 def execute run if @args.any? then u = @args.join " " puts "#{self.class::UNPROCA}: #{u}" end 0 rescue SignalException raise if @debug self.class.show_message $!.inspect 128 + ($!.signo||2) # Ruby 1.8 returns signo +nil+; assume SIGINT 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 47 def help c = self.class puts c.version puts puts c::DESCRIPTION puts c.show_options if block_given? then puts yield end raise Done end
option_act(args, opt, rest)
click to toggle source
# File lib/appl.rb, line 234 def option_act args, opt, rest dada = find_option_act opt dada or raise OptionError, "#{self::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
run()
click to toggle source
# File lib/appl.rb, line 73 def run end
show_message(msg, extra = nil)
click to toggle source
# File lib/appl.rb, line 272 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 246 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 " %-10s %-12s %s" % [ opt, arg, desc] end end
show_version()
click to toggle source
# File lib/appl.rb, line 256 def show_version puts version 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 end
unalias_option(opt)
click to toggle source
# File lib/appl.rb, line 173 def unalias_option opt self < Application or return superclass.unalias_option opt @aliases.delete opt nil end
version()
click to toggle source
# File lib/appl.rb, line 61 def version self.class.show_version raise Done end
Protected Instance Methods
all_aliases()
click to toggle source
# File lib/appl.rb, line 197 def all_aliases if self < Application then r = superclass.all_aliases r.update @aliases else {} end end
all_options()
click to toggle source
# File lib/appl.rb, line 188 def all_options if self < Application then r = superclass.all_options r.update @options else {} end end
find_option_act(opt)
click to toggle source
# File lib/appl.rb, line 182 def find_option_act opt self < Application or return @options[ opt] || @options[ @aliases[ opt]] || (superclass.find_option_act opt) end
options_desc() { |opt, arg, dfl, desc| ... }
click to toggle source
# File lib/appl.rb, line 206 def options_desc &block a = Hash.new do |h,k| h[ k] = [] end all_aliases.each { |k,v| a[ v].push k } a.values.each { |v| v.sort! } unless HASH_ORDER each_option { |opt,desc,arg,dfl,| yield opt, arg, dfl, desc a[ opt].each { |l| yield l, nil, nil, nil } } yield "", nil, nil, self::STOPOPT end
Private Instance Methods
attr_bang(*syms)
click to toggle source
# File lib/appl.rb, line 126 def attr_bang *syms syms.each { |sym| define_method :"#{sym}!" do instance_variable_set :"@#{sym}", true end } nil end
inherited(sub)
click to toggle source
# File lib/appl.rb, line 122 def inherited sub sub.instance_eval { @options, @aliases = {}, {} } end