Module: SanUltari::CommandDescriptor::ClassMethods

Defined in:
lib/sanultari/command_descriptor.rb

Instance Method Summary collapse

Instance Method Details

#available_commandsObject



51
52
53
# File 'lib/sanultari/command_descriptor.rb', line 51

def available_commands
  @registry.keys
end

#default(command) ⇒ Object



43
44
45
# File 'lib/sanultari/command_descriptor.rb', line 43

def default command
  @default_command = command.to_sym
end

#desc(command, description) ⇒ Object



16
17
# File 'lib/sanultari/command_descriptor.rb', line 16

def desc command, description
end

#get(command) ⇒ Object



47
48
49
# File 'lib/sanultari/command_descriptor.rb', line 47

def get command
  @registry[command.to_sym]
end

#group(group_name, clazz, operation = nil) ⇒ Object



40
41
# File 'lib/sanultari/command_descriptor.rb', line 40

def group group_name, clazz, operation = nil
end

#import(clazz, operation = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sanultari/command_descriptor.rb', line 26

def import clazz, operation = nil
  targets = operation
  targets ||= clazz.available_commands
  unless targets.kind_of? Array
    targets = [targets]
  end

  targets.each do |cmd|
    command = cmd.to_sym
    wrapper = clazz.get command
    @registry[command] = wrapper
  end
end

#listObject



55
56
57
# File 'lib/sanultari/command_descriptor.rb', line 55

def list

end

#map(command, clazz, options = nil) ⇒ Object



11
12
13
14
# File 'lib/sanultari/command_descriptor.rb', line 11

def map command, clazz, options = nil
  @registry ||= {}
  @registry[command.to_sym] = SanUltari::CommandWrapper.new command, clazz, nil, options
end

#option(command, option, options = nil) ⇒ Object



23
24
# File 'lib/sanultari/command_descriptor.rb', line 23

def option command, option, options = nil
end

#param(command, param, options = nil) ⇒ Object



19
20
21
# File 'lib/sanultari/command_descriptor.rb', line 19

def param command, param, options = nil
  @registry[command.to_sym].add_param param, options
end

#run(argv) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sanultari/command_descriptor.rb', line 59

def run argv
  selected_command = nil
  options = []
  args = []
  argument_list = argv.clone
  argv.each do |arg|
    if arg.start_with? '-'
      value = argument_list.shift
      args.push value
      options.push value
      next
    end

    unless @registry.include? arg.to_sym
      args.push argument_list.shift
      next
    end

    selected_command = @registry[argument_list.shift.to_sym]
  end

  args += argument_list
  if selected_command == nil
    options.clear
  else
    args -= options
  end

  selected_command ||= @registry[@default_command] unless @default_command == nil
  selected_command.run(args, options) if selected_command != nil
end