Module: DBA::Shell

Extended by:
Shell
Included in:
Shell
Defined in:
lib/dba/shell.rb

Instance Method Summary collapse

Instance Method Details

#run(args = ARGV) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dba/shell.rb', line 6

def run(args=ARGV)
  print_usage if args.empty? || args == ['--help']

  command_name = args.shift

  command = commands[command_name]

  if command.nil?
    raise DBA::Error, "#{command_name} is not a valid command"
  end

  command = DBA.const_get(command)

  arity = command.instance_method(:call).arity

  if arity >= 0 && args.size != arity
    raise DBA::Error, "incorrect number of args (given #{args.size}, expected #{arity})"
  end

  database = DBA::Database.connect

  command = command.new(database)
  command.call(*args)
rescue DBA::Error => exception
  print_error(exception.message)
end