Module: SanUltari::CommandDescriptor::ClassMethods
- Defined in:
- lib/sanultari/command_descriptor.rb
Instance Method Summary collapse
- #available_commands ⇒ Object
- #default(command) ⇒ Object
- #desc(command, description) ⇒ Object
- #get(command) ⇒ Object
- #group(group_name, clazz, operation = nil) ⇒ Object
- #import(clazz, operation = nil) ⇒ Object
- #list ⇒ Object
- #map(command, clazz, options = nil) ⇒ Object
- #option(command, option, options = nil) ⇒ Object
- #param(command, param, options = nil) ⇒ Object
- #run(argv) ⇒ Object
Instance Method Details
#available_commands ⇒ Object
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 |
#list ⇒ Object
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, = nil @registry ||= {} @registry[command.to_sym] = SanUltari::CommandWrapper.new command, clazz, nil, end |
#option(command, option, options = nil) ⇒ Object
23 24 |
# File 'lib/sanultari/command_descriptor.rb', line 23 def option command, option, = nil end |
#param(command, param, options = nil) ⇒ Object
19 20 21 |
# File 'lib/sanultari/command_descriptor.rb', line 19 def param command, param, = nil @registry[command.to_sym].add_param param, 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 = [] args = [] argument_list = argv.clone argv.each do |arg| if arg.start_with? '-' value = argument_list.shift args.push value .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 .clear else args -= end selected_command ||= @registry[@default_command] unless @default_command == nil selected_command.run(args, ) if selected_command != nil end |