module Dry::CLI::Registry

def self.extended(base)

Other tags:
    Api: - private

Other tags:
    Since: - 0.1.0
def self.extended(base)
  base.class_eval do
    @_mutex = Mutex.new
    @commands = CommandRegistry.new
  end
end

def _callback(callback, blk)

Other tags:
    Api: - private

Other tags:
    Since: - 0.2.0
def _callback(callback, blk)
  return blk if blk.respond_to?(:to_proc)
  case callback
  when ->(c) { c.respond_to?(:call) }
    callback.method(:call)
  when Class
    begin
      _callback(callback.new, blk)
    rescue ArgumentError
      raise InvalidCallbackError, callback
    end
  else
    raise InvalidCallbackError, callback
  end
end

def after(command_name, callback = nil, &blk)

Other tags:
    Example: Register a class as callback -
    Example: Register an object as callback -

Other tags:
    Since: - 0.2.0

Raises:
  • (Dry::CLI::InvalidCallbackError) - if the given callback doesn't
  • (Dry::CLI::UnknownCommandError) - if the command isn't registered

Parameters:
  • blk (Proc) -- the callback espressed as a block
  • callback (Class, #call) -- the callback object. If a class is given,
  • command_name (String) -- the name used for command registration
def after(command_name, callback = nil, &blk)
  @_mutex.synchronize do
    command(command_name).after_callbacks.append(&_callback(callback, blk))
  end
end

def before(command_name, callback = nil, &blk)

Other tags:
    Example: Register a class as callback -
    Example: Register an object as callback -

Other tags:
    Since: - 0.2.0

Raises:
  • (Dry::CLI::InvalidCallbackError) - if the given callback doesn't
  • (Dry::CLI::UnknownCommandError) - if the command isn't registered

Parameters:
  • blk (Proc) -- the callback espressed as a block
  • callback (Class, #call) -- the callback object. If a class is given,
  • command_name (String) -- the name used for command registration
def before(command_name, callback = nil, &blk)
  @_mutex.synchronize do
    command(command_name).before_callbacks.append(&_callback(callback, blk))
  end
end

def command(command_name)

Other tags:
    Api: - private

Other tags:
    Since: - 0.2.0
def command(command_name)
  get(command_name.split(COMMAND_NAME_SEPARATOR)).tap do |result|
    raise UnknownCommandError, command_name unless result.found?
  end
end

def get(arguments)

Other tags:
    Api: - private

Other tags:
    Since: - 0.1.0
def get(arguments)
  @commands.get(arguments)
end

def register(name, command = nil, aliases: [], hidden: false, &block)

Other tags:
    Example: Register a group of commands -
    Example: Register a command with aliases -
    Example: Register a command -

Other tags:
    Since: - 0.1.0

Parameters:
  • options (Hash) -- a set of options
  • aliases (Array) -- an optional list of aliases
  • command (NilClass, Dry::CLI::Command) -- the optional command
  • name (String) -- the command name
def register(name, command = nil, aliases: [], hidden: false, &block)
  @commands.set(name, command, aliases, hidden)
  if block_given?
    prefix = Prefix.new(@commands, name, aliases, hidden)
    if block.arity.zero?
      prefix.instance_eval(&block)
    else
      yield(prefix)
    end
  end
end