lib/cl/cmd.rb in cl-0.0.2 vs lib/cl/cmd.rb in cl-0.0.3

- old
+ new

@@ -1,28 +1,50 @@ +require 'cl/args' require 'cl/registry' module Cl - module Cmd - def self.included(const) - const.send :include, Registry - const.send :extend, ClassMethods - end + class Cmd < Struct.new(:args, :opts) + include Registry - module ClassMethods + class << self + def inherited(cmd) + cmd.register underscore(cmd.name.split('::').last) + end + def args(*args) - args.any? ? @args = args : @args ||= [] + return @args ||= Args.new unless args.any? + opts = args.last.is_a?(Hash) ? args.pop : {} + args.each { |arg| arg(arg, opts) } end - def purpose(purpose = nil) - purpose ? @purpose = purpose : @purpose + def arg(name, opts = {}) + args.define(self, name, opts) end - def on(*args, &block) + def cmd(summary = nil) + @summary = summary + end + + attr_reader :summary + + def opt(*args, &block) opts << [args, block] end def opts - @opts ||= superclass.respond_to?(:opts) ? superclass.opts : [] + @opts ||= superclass != Cmd && superclass.respond_to?(:opts) ? superclass.opts.dup : [] end + + def underscore(string) + string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). + gsub(/([a-z\d])([A-Z])/,'\1_\2'). + downcase + end + end + + def initialize(args, opts) + args = self.class.args.apply(self, args) + opts = self.class::OPTS.merge(opts) if self.class.const_defined?(:OPTS) + super end end end