Class: Clitopic::Command::Base
- Inherits:
-
Object
- Object
- Clitopic::Command::Base
- Extended by:
- Parser::OptParser
- Defined in:
- lib/clitopic/command/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.arguments ⇒ Object
Returns the value of attribute arguments.
-
.banner ⇒ Object
Returns the value of attribute banner.
-
.description ⇒ Object
Returns the value of attribute description.
-
.hidden ⇒ Object
Returns the value of attribute hidden.
-
.inherited_options(*cmds) ⇒ Object
Returns the value of attribute inherited_options.
-
.name ⇒ Object
Returns the value of attribute name.
-
.options ⇒ Object
Returns the value of attribute options.
-
.short_description ⇒ Object
Returns the value of attribute short_description.
Class Method Summary collapse
- .call ⇒ Object
- .cmd_options ⇒ Object
- .fullname ⇒ Object
- .load_defaults(file = nil) ⇒ Object
- .load_options(opts) ⇒ Object
- .option(name, *args, &blk) ⇒ Object
- .register(opts = {}) ⇒ Object
- .topic(arg = nil) ⇒ Object
- .topic_options ⇒ Object
Methods included from Parser::OptParser
check_all_required, check_required, help, parse, parser, process_options
Class Attribute Details
.arguments ⇒ Object
Returns the value of attribute arguments.
12 13 14 |
# File 'lib/clitopic/command/base.rb', line 12 def arguments @arguments end |
.banner ⇒ Object
Returns the value of attribute banner.
11 12 13 |
# File 'lib/clitopic/command/base.rb', line 11 def @banner end |
.description ⇒ Object
Returns the value of attribute description.
11 12 13 |
# File 'lib/clitopic/command/base.rb', line 11 def description @description end |
.hidden ⇒ Object
Returns the value of attribute hidden.
11 12 13 |
# File 'lib/clitopic/command/base.rb', line 11 def hidden @hidden end |
.inherited_options(*cmds) ⇒ Object
Returns the value of attribute inherited_options.
12 13 14 |
# File 'lib/clitopic/command/base.rb', line 12 def @inherited_options end |
.name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/clitopic/command/base.rb', line 11 def name @name end |
.options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/clitopic/command/base.rb', line 12 def @options end |
.short_description ⇒ Object
Returns the value of attribute short_description.
11 12 13 |
# File 'lib/clitopic/command/base.rb', line 11 def short_description @short_description end |
Class Method Details
.call ⇒ Object
53 54 55 |
# File 'lib/clitopic/command/base.rb', line 53 def call() puts "call with #{} #{arguments}" end |
.cmd_options ⇒ Object
14 15 16 |
# File 'lib/clitopic/command/base.rb', line 14 def @cmd_options ||= [] end |
.fullname ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/clitopic/command/base.rb', line 43 def fullname if topic.nil? return name elsif name == 'index' "#{topic.name}" else "#{topic.name}:#{name}" end end |
.load_defaults(file = nil) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/clitopic/command/base.rb', line 87 def load_defaults(file=nil) file ||= Clitopic::Helpers.find_default_file if file.nil? return end begin defaults = YAML.load_file(file) rescue puts "failed to load defaults file: #{file}" return end if self.topic.nil? if defaults.has_key?("commands") cmd_defaults = defaults["commands"][self.name] end else if defaults.has_key?("topics") && defaults["topics"].has_key?(self.topic.name) cmd_defaults = defaults['topics'][self.topic.name][self.name] topic_opts = defaults['topics'][self.topic.name]['topic_options'] (topic_opts) if !cmd_defaults.nil? (cmd_defaults["options"]) if cmd_defaults["args"] && !arguments @arguments += Array(cmd_defaults["args"]) end end self..each do |cmd| if defaults["topics"].has_key?(self.topic.name) cmd_opts = defaults['topics'][self.topic.name][cmd.to_s] (cmd_opts["options"]) if !cmd_opts.nil? end end end end end |
.load_options(opts) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/clitopic/command/base.rb', line 73 def (opts) return if opts.nil? opts.each do |name, value| name = name.to_s.to_sym if !value.nil? if [name].is_a?(Array) [name] += value else [name] = value end end end end |
.option(name, *args, &blk) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/clitopic/command/base.rb', line 35 def option(name, *args, &blk) opt = Clitopic::Utils.parse_option(name, *args, &blk) if !opt[:default].nil? [name] = opt[:default] end << opt end |
.register(opts = {}) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/clitopic/command/base.rb', line 140 def register(opts={}) opts = {hidden: false}.merge(opts) if !opts.has_key?(:name) raise ArgumentError.new("missing Command name") end topic(opts[:topic]) @description = opts[:description] @name = opts[:name] @hidden = opts[:hidden] @banner = opts[:banner] @short_description = opts[:short_description] if @topic.nil? Clitopic::Commands.global_commands[name] = self else topic.commands[name] = self end end |
.topic(arg = nil) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/clitopic/command/base.rb', line 126 def topic(arg=nil) if !arg.nil? if arg.is_a?(String) @topic ||= Topics[arg] elsif arg.is_a?(Class) && arg < Clitopic::Topic::Base @topic ||= arg.instance elsif arg.is_a?(Hash) @topic ||= Clitopic::Topic::Base.register(arg) end end return @topic end |
.topic_options ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/clitopic/command/base.rb', line 61 def if !self.topic.nil? @topic_options ||= self.topic.class. else @topic_options = {} end end |