Class: Commander

Inherits:
Object
  • Object
show all
Defined in:
lib/core/commander.rb

Instance Method Summary collapse

Constructor Details

#initializeCommander

Returns a new instance of Commander.



5
6
7
# File 'lib/core/commander.rb', line 5

def initialize
  @commands = Command.load_all
end

Instance Method Details

#goObject



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/core/commander.rb', line 9

def go
  if ARGV.first == '--version'
    ARGV[0] = 'version' # simulate 'script --version' functionality
  end

  if ARGV.first == '-h' || ARGV.first == '--help'
    ARGV[0] = 'help' # catch help requests.
  end

  if Rcli.script_config['global']['mode'] == 'multi' && ARGV.size == 0 
    ARGV.push Command.default_cmd # default action
  end
  

  if Rcli.script_config['global']['mode'] == 'single'
    if ARGV[0] != 'help' && ARGV[0] != 'version' && ARGV[0] != 'debug'
      ARGV.insert( 0, Command.default_cmd)
    end
  elsif ARGV.first[0,1] !~ /^[a-zA-z]$/ 
    puts "ERROR: Please specify a command as first argument"
    exit
  end

  command = ARGV.shift # first parameter should be the command at this point.

  unless @commands.keys.include?(command)
    puts "ERROR: Invalid command: "  + command + ". Please use 'help' command for a list of allowed commands."
    exit
  end

  # Separate arguments into those preceded by dashes (usually options or flags)
  # and those not preceded by dashes (usually files)
  opts = ARGV.collect { |arg| arg if arg[0,1] == '-' }.compact
  args = ARGV.collect { |arg| arg if arg[0,1] != '-'}.compact

  @commands[command][:instance].run(:opts => opts,:args =>args)
  
end