Module: Sickle::ClassMethods
- Defined in:
- lib/sickle.rb
Instance Method Summary collapse
- #__commands ⇒ Object
- #__global_options ⇒ Object
- #before(&block) ⇒ Object
- #desc(label) ⇒ Object
- #flag(name) ⇒ Object
- #global_flag(name) ⇒ 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
225 226 227 |
# File 'lib/sickle.rb', line 225 def __commands @__commands ||= {} end |
#__global_options ⇒ Object
229 230 231 |
# File 'lib/sickle.rb', line 229 def ||= {} end |
#before(&block) ⇒ Object
193 194 195 |
# File 'lib/sickle.rb', line 193 def before(&block) @__before_hook = block end |
#desc(label) ⇒ Object
197 198 199 |
# File 'lib/sickle.rb', line 197 def desc(label) Sickle.push_desc(label) end |
#flag(name) ⇒ Object
213 214 215 |
# File 'lib/sickle.rb', line 213 def flag(name) option(name, :default => false) end |
#global_flag(name) ⇒ Object
205 206 207 |
# File 'lib/sickle.rb', line 205 def global_flag(name) global_option(name, :default => false) end |
#global_option(name, opts = {}) ⇒ Object
201 202 203 |
# File 'lib/sickle.rb', line 201 def global_option(name, opts = {}) [name.to_s] = Option.new(name, opts) end |
#include_modules(hash) ⇒ Object
217 218 219 220 221 222 223 |
# 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) ⇒ 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
285 286 287 288 289 290 |
# 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.) end end |
#option(name, opts = {}) ⇒ Object
209 210 211 |
# File 'lib/sickle.rb', line 209 def option(name, opts = {}) Sickle.push_option(name, opts) end |
#run(argv) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# 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 = .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 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 |