module Sickle::ClassMethods
Public Instance Methods
__commands()
click to toggle source
# File lib/sickle.rb, line 225 def __commands @__commands ||= {} end
__global_options()
click to toggle source
# File lib/sickle.rb, line 229 def __global_options @__global_options ||= {} end
before(&block)
click to toggle source
# File lib/sickle.rb, line 193 def before(&block) @__before_hook = block end
desc(label)
click to toggle source
# File lib/sickle.rb, line 197 def desc(label) Sickle.push_desc(label) end
flag(name)
click to toggle source
# File lib/sickle.rb, line 213 def flag(name) option(name, :default => false) end
global_flag(name)
click to toggle source
# File lib/sickle.rb, line 205 def global_flag(name) global_option(name, :default => false) end
global_option(name, opts = {})
click to toggle source
# File lib/sickle.rb, line 201 def global_option(name, opts = {}) __global_options[name.to_s] = Option.new(name, opts) end
include_modules(hash)
click to toggle source
# File lib/sickle.rb, line 217 def include_modules(hash) hash.each do |key, value| Sickle.push_namespace(key) send(:include, value) Sickle.pop_namespace end end
included(base)
click to toggle source
# 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)
click to toggle source
# File lib/sickle.rb, line 285 def method_added(a) meth = instance_method(a) if desc = Sickle.pop_desc __commands[a.to_s] = Command.new(meth, a, desc, Sickle.pop_options) end end
option(name, opts = {})
click to toggle source
# File lib/sickle.rb, line 209 def option(name, opts = {}) Sickle.push_option(name, opts) end
run(argv)
click to toggle source
# File lib/sickle.rb, line 233 def run(argv) # puts "ARGV: #{argv.inspect}" if command_name = argv.shift command_name = command_name.gsub("-", "_") if command = __commands[command_name] all = __global_options.values + command.options.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 obj = self.new obj.instance_variable_set(:@__options, results) if @__before_hook obj.instance_exec(&@__before_hook) end req, opt = command.meth.parameters.partition {|(r,_)| r == :req } r,o = req.size, opt.size argr = (r..(r+o)) if argr.include?(args.size) command.meth.bind(obj).call(*args) else puts "\e[31mWrong number of arguments (#{args.size} for #{argr})\e[0m" puts run(["help", command_name]) exit 127 end else puts "\e[31mCommand '#{command_name}' not found\e[0m" puts run(["help"]) exit 127 end else run(["help"]) end end