Module: Ext::Command::CClassMethods

Defined in:
lib/ext/command.rb

Instance Method Summary collapse

Instance Method Details

#command(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ext/command.rb', line 9

def command(*args)
  self::X.M
  args.flatten!
  cmds=self::X.r.select{|k|k.instance_methods.include?'cmd'}
  cmd=args.shift

  # Default actions
  unless cmd
    cmd='default' if cmds.find{|k| /^default$/i =~ k.name.demodulize}
    cmd='list' unless cmd
  end

  case cmd
  when /^list$/i
    cmds.each do |k|
      puts k.name.demodulize.methodize
      puts k.help if k.respond_to? :help
    end
    exit
  else
    k=cmds.find{|k| %r{^#{cmd.gsub('_','')}$}i =~ k.name.demodulize}
    if not k
      puts "ERROR : No command named '#{cmd}'"; exit -1
    end

    out = send(:cmd, k.name.demodulize, *args).body

    case out
    when String
      puts out
    else
      pp out
    end
  end
rescue => ex
  puts "ERROR : #{ex.message}"
  puts ex.backtrace.join("\n") if $DBG
end