Sha256: bc9b10d8b527ae1f7b2b31c04e76508337cb4f4840f15c9b5b7461b327f84d84

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require 'cl/args'
require 'cl/registry'

module Cl
  class Cmd < Struct.new(:args, :opts)
    include Registry

    class << self
      def inherited(cmd)
        cmd.register underscore(cmd.name.split('::').last)
      end

      def 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 arg(name, opts = {})
        args.define(self, name, opts)
      end

      def purpose(purpose = nil)
        purpose ? @purpose = purpose : @purpose
      end

      def opt(*args, &block)
        opts << [args, block]
      end

      def 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cl-0.0.4 lib/cl/cmd.rb