Sha256: 65da1c74955f8de388e3dd7d90111410e3cbe4a4680a6676c6ff861aee5ccc91

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module Cogy
  class Command
    attr :name, :args, :opts, :desc, :long_desc, :example, :rules, :handler

    def initialize(name, args: [], opts: {}, desc:, long_desc: nil, example: nil, rules: nil)
      @name = name
      @args = [args].flatten.map!(&:to_s)
      @opts = opts.with_indifferent_access
      @desc = desc
      @long_desc = long_desc
      @example = example
      @rules = rules || ["allow"]
    end

    def register!(handler)
      if Cogy.commands[name]
        raise "A command with the name #{name} is already registered"
      end

      @handler = handler
      @handler.command = self

      Cogy.commands[name] = self
    end

    def run!(*args)
      handler.run(*args)
    end

    # Suitable for bundle config display
    def formatted_args
      args.map { |a| "<#{a}>" }.join(" ")
    end

    # Suitable for bundle config display.
    #
    # Get rid of HashWithIndifferentAccess, otherwise the resulting YAML
    # will contain garbage.
    def formatted_opts
      opts.to_hash
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cogy-0.0.2 lib/cogy/command.rb
cogy-0.0.1 lib/cogy/command.rb