Sha256: 668a3e8b093041e7f26eee3e7bbad90fd34a9d281365484c3d8dd944efe8a659

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module Clamp

  class Subcommand < Struct.new(:name, :description, :subcommand_class)

    def help
      [name, description]
    end

    module Declaration

      def recognised_subcommands
        @recognised_subcommands ||= []
      end

      def subcommand(name, description, subcommand_class = self, &block)
        has_subcommands!
        if block
          # generate a anonymous sub-class
          subcommand_class = Class.new(subcommand_class, &block)
        end
        recognised_subcommands << Subcommand.new(name, description, subcommand_class)
      end

      def has_subcommands?
        !recognised_subcommands.empty?
      end

      def find_subcommand(name)
        recognised_subcommands.find { |sc| sc.name == name }
      end
      
      def has_subcommands!
        unless @has_subcommands
          parameter "SUBCOMMAND", "subcommand name", :attribute_name => :subcommand_name
          parameter "[ARGS] ...", "subcommand arguments", :attribute_name => :subcommand_arguments
          @has_subcommands = true
        end
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clamp-0.1.2 lib/clamp/subcommand/declaration.rb