Sha256: 20c3729b1d31d9cdf5e32be418c93a7af857366fbc823998cd3cbf15e31a9d23

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'applix/hash'

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...>

--- #{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)
  end

  private 

  def handle name, &blk
    tasks[name.to_sym] = { :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__

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
applix-0.3.0 lib/applix.rb