Class: Noir::Executer

Inherits:
Object
  • Object
show all
Defined in:
lib/noir/executer.rb

Class Method Summary collapse

Class Method Details

.args_from_argvObject



44
45
46
47
48
49
50
# File 'lib/noir/executer.rb', line 44

def self.args_from_argv
  argv         = ARGV.clone
  command_str  = self.command_from_argv.to_s.sub(/^Noir::Command/, '')
  command_str  = command_str.sub(/^::/, '')
  command_size = command_str.split('::').size
  return argv.drop(command_size)
end

.command_from_argvObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/noir/executer.rb', line 30

def self.command_from_argv
  args          = ARGV.clone
  command_arr   = ['Noir', 'Command']   # command prefix and default command

  while true
    break unless search_str      = args.shift
    break unless matched_command = find_command(command_arr, search_str)

    command_arr << matched_command
  end

  return eval(command_arr.join('::'))
end

.executeObject

inherited methods



54
55
56
57
58
59
60
61
62
# File 'lib/noir/executer.rb', line 54

def self.execute
  Noir::Options.parse_options_from_argv!

  if Noir::Options.exist? Noir::Options::Help
    return command_from_argv.description
  end

  command_from_argv.execute(*args_from_argv)
end

.find_abbr_command(commands, search_str) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/noir/executer.rb', line 18

def self.find_abbr_command commands, search_str
  # abbrev match
  matched_commands = commands.select{|c| c.downcase.start_with? search_str}

  return nil if matched_commands.empty?
  unless matched_commands.size == 1
    raise "#{search_str} is ambiguous. matched #{matched_commands}"
  end

  matched_commands.first
end

.find_command(command_arr, search_str) ⇒ Object

utils



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/noir/executer.rb', line 6

def self.find_command command_arr, search_str
  command = eval(command_arr.join('::'))

  commands    = command.sub_commands.map(&:to_s)
  matched_str = commands.find{|c| c.downcase == search_str.downcase}
  matched_str = find_abbr_command(commands, search_str) if matched_str.nil?

  return nil if matched_str.nil?

  return matched_str
end