lib/applix.rb in applix-0.3.6 vs lib/applix.rb in applix-0.3.7

- old
+ new

@@ -2,12 +2,15 @@ class Applix def self.main argv, defaults = {}, &blk app = Applix.new app.instance_eval(&blk) - app.run(argv, defaults) + app.run(argv, defaults, &blk) + end + def self.main! argv, defaults = {}, &blk + self.main argv, defaults, &blk rescue => e puts <<-EOT ## #{e} @@ -23,19 +26,35 @@ args = (options.delete :args) # pre handle, can modify args & options @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 + # logic table for dispatching the command line onto an action + # + # id | name exits? any | action + # -- | -----------------+-------------- + # 1 | - - | error: no any, mapped to #3 with name == :any + # 2 | - x | -> any + # 3 | x - - | error: no any + # 4 | x - x | -> any + # 5 | x x - | -> task + # 6 | x x x | -> task + # + # having name with no task is the same as no name with no any task.. + name = (args.shift || :any).to_sym + # shoose existing task or :any + task = tasks[name] || tasks[:any] + task or (raise "no such task: '#{name}'") + + # case #4: we must un-shift the name back into the args list to lets any + # see it as its first argument, + (args.unshift name.to_s) if(name != :any && task[:name] == :any) + + # do the call + rc = task[:code].call(*args, options) + # post handle unless @epilog_cb.nil? rc = @epilog_cb.call(rc, args, options) end @@ -51,51 +70,16 @@ def epilog &blk @epilog_cb = blk end def any &blk - tasks[:any] = { :code => blk } + tasks[:any] = { :name => :any, :code => blk } end def handle name, &blk - tasks[name.to_sym] = { :code => blk } + tasks[name.to_sym] = { :name => name, :code => blk } end def tasks @tasks ||= {} end end - -__END__ -# -def main args, options = {} - options = (Defaults.merge options) - options[:date] = Date.parse(options[:date]) # up-type string date - - action = args.shift or raise "no action" - - # account is an command line arg but password is prompted, never have that in - # a config or on the command line! - # - username = args.shift # or raise "no username" - password = prompt_for_password - - # which method to run depend on first command line argument.. - send action, username, password, options -end - -params = Hash.from_argv ARGV -begin - main params[:args], params -rescue => e - puts <<-EOT - -## #{e} - -usage: #{__FILE__} <task> <username> - ---- #{e.backtrace.join "\n "} - EOT -end - -__END__ -