Module: Sickle::ClassMethods
- Defined in:
- lib/sickle.rb
Instance Method Summary collapse
- #__commands ⇒ Object
- #__global_options ⇒ Object
- #desc(label) ⇒ Object
- #global_option(name, opts = {}) ⇒ Object
- #include_modules(hash) ⇒ Object
- #included(base) ⇒ Object
- #method_added(a) ⇒ Object
- #option(name, opts = {}) ⇒ Object
- #run(argv) ⇒ Object
Instance Method Details
#__commands ⇒ Object
213 214 215 |
# File 'lib/sickle.rb', line 213 def __commands @__commands ||= {} end |
#__global_options ⇒ Object
217 218 219 |
# File 'lib/sickle.rb', line 217 def ||= {} end |
#desc(label) ⇒ Object
193 194 195 |
# File 'lib/sickle.rb', line 193 def desc(label) Sickle.push_desc(label) end |
#global_option(name, opts = {}) ⇒ Object
197 198 199 |
# File 'lib/sickle.rb', line 197 def global_option(name, opts = {}) [name.to_s] = Option.new(name, opts) end |
#include_modules(hash) ⇒ Object
205 206 207 208 209 210 211 |
# File 'lib/sickle.rb', line 205 def include_modules(hash) hash.each do |key, value| Sickle.push_namespace(key) send(:include, value) Sickle.pop_namespace end end |
#included(base) ⇒ Object
186 187 188 189 190 191 |
# File 'lib/sickle.rb', line 186 def included(base) __commands.each do |name, command| name = (Sickle.namespace + [name]).join(":") base.__commands[name] = command end end |
#method_added(a) ⇒ Object
258 259 260 261 262 263 |
# File 'lib/sickle.rb', line 258 def method_added(a) meth = instance_method(a) if desc = Sickle.pop_desc __commands[a.to_s] = Command.new(meth, a, desc, Sickle.) end end |
#option(name, opts = {}) ⇒ Object
201 202 203 |
# File 'lib/sickle.rb', line 201 def option(name, opts = {}) Sickle.push_option(name, opts) end |
#run(argv) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/sickle.rb', line 221 def run(argv) # puts "ARGV: #{argv.inspect}" if command_name = argv.shift if command = __commands[command_name] all = .values + command..values results = {} args = OptionParser.new do |parser| all.each do |option| option.register(parser, results) end end.parse!(argv) all.each do |o| results[o.name] ||= o.default end # puts "args: #{args.inspect}" # puts "results: #{results.inspect}" obj = self.new obj.instance_variable_set(:@__options, results) command.meth.bind(obj).call(*args) else puts "\e[31mCommand '#{command_name}' not found\e[0m" puts run(["help"]) end else run(["help"]) end end |