lib/applix.rb in applix-0.3.0 vs lib/applix.rb in applix-0.3.4

- old
+ new

@@ -3,32 +3,59 @@ class Applix def self.main argv, defaults = {}, &blk app = Applix.new app.instance_eval(&blk) app.run(argv, defaults) + rescue => e puts <<-EOT ## #{e} -usage: #{__FILE__} <args...> +usage: #{$0} <args...> --- #{e.backtrace.join "\n "} EOT end def run argv, defaults options = (Hash.from_argv argv) options = (defaults.merge options) args = (options.delete :args) - # which task to run depends on first line argument.. - (name = args.shift) or (raise "no task") - (task = tasks[name.to_sym]) or (raise "no such task: '#{name}'") - task[:code].call(*args, options) + # pre handle + @prolog_cb.call(*args, options) unless @prolog_cb.nil? + + # it's either :any + if task = tasks[:any] + rc = task[:code].call(*args, options) + else # or the task defined by the first argument + (name = args.shift) or (raise "no task") + (task = tasks[name.to_sym]) or (raise "no such task: '#{name}'") + rc = task[:code].call(*args, options) + end + + # post handle + unless @epilog_cb.nil? + rc = @epilog_cb.call(rc, *args, options) + end + + rc # return result code from handle callbacks, not the epilog_cb end private + + def prolog &blk + @prolog_cb = blk + end + + def epilog &blk + @epilog_cb = blk + end + + def any &blk + tasks[:any] = { :code => blk } + end def handle name, &blk tasks[name.to_sym] = { :code => blk } end