Sha256: 3ed8d9ca5ed133d2bd68b5a4276e056309e55758a342eae9aa006a7a2fc5706b

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

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

    def initialize(name, args: [], opts: {}, desc:, long_desc: nil, examples: nil, rules: nil)
      @name = name
      @args = [args].flatten.map!(&:to_s)
      @opts = opts.with_indifferent_access
      @desc = desc
      @long_desc = long_desc
      @examples = examples
      @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

1 entries across 1 versions & 1 rubygems

Version Path
cogy-0.0.3 lib/cogy/command.rb