Module: RCoLi::CommandContainer
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #action(&block) ⇒ Object
- #command(nam, &block) ⇒ Object
- #commands ⇒ Object
- #find_command(name) ⇒ Object
- #flag(names, &block) ⇒ Object
- #get_action ⇒ Object
- #options ⇒ Object
- #parse_args(args, result) ⇒ Object
- #put_default_values(result) ⇒ Object
- #switch(names, &block) ⇒ Object
- #validate_options(result, target) ⇒ Object
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
14 15 16 |
# File 'lib/rcoli/model.rb', line 14 def parent @parent end |
Instance Method Details
#action(&block) ⇒ Object
17 18 19 |
# File 'lib/rcoli/model.rb', line 17 def action(&block) @action = block end |
#command(nam, &block) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/rcoli/model.rb', line 25 def command(nam, &block) obj = Command.new(nam) obj.parent = self block.call(obj) commands << obj end |
#commands ⇒ Object
32 33 34 |
# File 'lib/rcoli/model.rb', line 32 def commands (@commands ||= []) end |
#find_command(name) ⇒ Object
90 91 92 93 |
# File 'lib/rcoli/model.rb', line 90 def find_command(name) result = commands.find{|command| command.value_of_name.to_s.eql? name} return result end |
#flag(names, &block) ⇒ Object
46 47 48 49 50 |
# File 'lib/rcoli/model.rb', line 46 def flag(names, &block) obj = Flag.new(names) block.call(obj) if block_given? << obj end |
#get_action ⇒ Object
21 22 23 |
# File 'lib/rcoli/model.rb', line 21 def get_action @action end |
#options ⇒ Object
36 37 38 |
# File 'lib/rcoli/model.rb', line 36 def (@options ||= []) end |
#parse_args(args, result) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rcoli/model.rb', line 52 def parse_args(args, result) return if args.empty? arg = args.delete_at(0) if (is_option? arg) if (option = find_option(arg)) if (option.is_a? Flag) raise InvalidCommand, "Flag #{arg} is missing a value" if args.empty? value = args.delete_at(0) else value = true end target = self.parent ? :options : :global_options option.keys.each{|key| result.send(target)[key] = value} else raise InvalidCommand, "'#{arg}' is not a valid option" end else if (cmd = find_command(arg)) result.command = cmd cmd.put_default_values(result) cmd.parse_args(args, result) cmd.(result, :options) elsif (commands.empty?) result.arguments << arg else raise InvalidCommand, "'#{arg}' is not a valid #{@name} command" end end parse_args(args, result) end |
#put_default_values(result) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/rcoli/model.rb', line 83 def put_default_values(result) .find_all{|option| option.respond_to? :value_of_default_value and option.value_of_default_value}.each do |option| target = self.parent ? :options : :global_options option.keys.each{|key| result.send(target)[key] = option.value_of_default_value} end end |
#switch(names, &block) ⇒ Object
40 41 42 43 44 |
# File 'lib/rcoli/model.rb', line 40 def switch(names, &block) obj = Switch.new(names) block.call(obj) if block_given? << obj end |
#validate_options(result, target) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/rcoli/model.rb', line 95 def (result, target) if (result.command.nil? or result.command.value_of_force == true) return else self..find_all{|o| o.is_a? Flag and o.value_of_required}.each do |o| raise InvalidCommand, "Required option '#{o.to_s}' is missing" unless o.keys.find{|key| result.send(target)[key]} end end end |