Sha256: 2d99b9625c51210f9e3d0b0cd52fa4369da0b1baff153cafa5b411163f0df5d8

Contents?: true

Size: 1.66 KB

Versions: 4

Compression:

Stored size: 1.66 KB

Contents

require 'clamp/subcommand'

module Clamp
  class Subcommand

    module Declaration

      def recognised_subcommands
        @recognised_subcommands ||= []
      end

      def subcommand(name, description, subcommand_class = self, &block)
        has_subcommands!
        declare_subcommand(name, description, subcommand_class, &block)
      end

      def default_subcommand(name, description, subcommand_class = self, &block)
        has_subcommands!(name)
        declare_subcommand(name, description, subcommand_class, &block)
      end

      def has_subcommands?
        @has_subcommands
      end

      def find_subcommand(name)
        recognised_subcommands.find { |sc| sc.is_called?(name) }
      end
      
      def has_subcommands!(default = nil)
        if @has_subcommands
          if default
            raise "You must declare the default_subcommand before any other subcommands"
          end
        else
          if default
            parameter "[SUBCOMMAND]", "subcommand name", :attribute_name => :subcommand_name, :default => default
          else
            parameter "SUBCOMMAND", "subcommand name", :attribute_name => :subcommand_name
          end
          parameter "[ARGS] ...", "subcommand arguments", :attribute_name => :subcommand_arguments
          @has_subcommands = true
        end
      end
      
      private
      
      def declare_subcommand(name, description, subcommand_class = self, &block)
        if block
          # generate a anonymous sub-class
          subcommand_class = Class.new(subcommand_class, &block)
        end
        recognised_subcommands << Subcommand.new(name, description, subcommand_class)
      end

    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
clamp-0.2.3 lib/clamp/subcommand/declaration.rb
clamp-0.2.2 lib/clamp/subcommand/declaration.rb
clamp-0.2.1 lib/clamp/subcommand/declaration.rb
clamp-0.2.0 lib/clamp/subcommand/declaration.rb