class Dry::CLI

@since 0.1.0
General purpose Command Line Interface (CLI) framework for Ruby

def self.command?(command)

Other tags:
    Api: - private

Other tags:
    Since: - 0.1.0

Returns:
  • (TrueClass, FalseClass) - true if instance of `Dry::CLI::Command`

Parameters:
  • command (Object) -- the command to check
def self.command?(command)
  case command
  when Class
    command.ancestors.include?(Command)
  else
    command.is_a?(Command)
  end
end

def anonymous_registry(&block)

Other tags:
    Api: - private

Other tags:
    Since: - 0.4.0

Returns:
  • (Module) - module extended with registry abilities and configured with a block

Parameters:
  • &block (Block) -- configuration for the registry
def anonymous_registry(&block)
  registry = Module.new { extend(Dry::CLI::Registry) }
  if block.arity.zero?
    registry.instance_eval(&block)
  else
    yield(registry)
  end
  registry
end

def build_command(command)

Other tags:
    Api: - private

Other tags:
    Since: - 0.6.0
def build_command(command)
  command.is_a?(Class) ? command.new : command
end

def call(arguments: ARGV, out: $stdout, err: $stderr)

Other tags:
    Since: - 0.1.0

Parameters:
  • err (IO) -- the error output (defaults to `$stderr`)
  • out (IO) -- the standard output (defaults to `$stdout`)
  • arguments (Array) -- the command line arguments (defaults to `ARGV`)
def call(arguments: ARGV, out: $stdout, err: $stderr)
  @out, @err = out, err
  kommand ? perform_command(arguments) : perform_registry(arguments)
rescue SignalException => exception
  signal_exception(exception)
rescue Errno::EPIPE
  # no op
end

def command?(command)

Other tags:
    See: .command? -

Other tags:
    Api: - private

Other tags:
    Since: - 0.1.0

Returns:
  • (TrueClass, FalseClass) - true if instance of `Dry::CLI::Command`

Parameters:
  • command (Object) -- the command to check
def command?(command)
  CLI.command?(command)
end

def error(result)

Other tags:
    Api: - private

Other tags:
    Since: - 0.6.0
def error(result)
  err.puts(result.error)
  exit(1)
end

def help(command, prog_name)

Other tags:
    Api: - private

Other tags:
    Since: - 0.6.0
def help(command, prog_name)
  out.puts Banner.call(command, prog_name)
  exit(0) # Successful exit
end

def initialize(command_or_registry = nil, &block)

Other tags:
    Since: - 0.1.0

Returns:
  • (Dry::CLI) - the new instance

Parameters:
  • &block (Block) -- a configuration block for registry
  • command_or_registry (Dry::CLI::Registry, Dry::CLI::Command) --
def initialize(command_or_registry = nil, &block)
  @kommand = command_or_registry if command?(command_or_registry)
  @registry =
    if block_given?
      anonymous_registry(&block)
    else
      command_or_registry
    end
end

def parse(command, arguments, names)

Other tags:
    Api: - private

Other tags:
    Since: - 0.6.0

Returns:
  • (Array) - returns an array where the

Parameters:
  • out (IO) -- sta output
  • result (Dry::CLI::CommandRegistry::LookupResult) --
def parse(command, arguments, names)
  prog_name = ProgramName.call(names)
  result = Parser.call(command, arguments, prog_name)
  return help(command, prog_name) if result.help?
  return error(result) if result.error?
  [build_command(command), result.arguments]
end

def perform_command(arguments)

Other tags:
    Api: - private

Other tags:
    Since: - 0.6.0

Parameters:
  • out (IO) -- the standard output (defaults to `$stdout`)
  • arguments (Array) -- the command line arguments
def perform_command(arguments)
  command, args = parse(kommand, arguments, [])
  command.instance_variable_set(:@err, err) unless command.instance_variable_defined?(:@err)
  command.instance_variable_set(:@out, out) unless command.instance_variable_defined?(:@out)
  command.call(**args)
end

def perform_registry(arguments)

Other tags:
    Api: - private

Other tags:
    Since: - 0.6.0

Parameters:
  • out (IO) -- the standard output (defaults to `$stdout`)
  • arguments (Array) -- the command line arguments
def perform_registry(arguments)
  result = registry.get(arguments)
  return spell_checker(result, arguments) unless result.found?
  command, args = parse(result.command, result.arguments, result.names)
  command.instance_variable_set(:@err, err) unless command.instance_variable_defined?(:@err)
  command.instance_variable_set(:@out, out) unless command.instance_variable_defined?(:@out)
  result.before_callbacks.run(command, **args)
  command.call(**args)
  result.after_callbacks.run(command, **args)
end

def signal_exception(exception)

Other tags:
    Api: - private

Other tags:
    Since: - 0.7.0
def signal_exception(exception)
  exit(128 + exception.signo)
end

def spell_checker(result, arguments)

Other tags:
    Since: - 1.1.1
def spell_checker(result, arguments)
  spell_checker = SpellChecker.call(result, arguments)
  err.puts spell_checker if spell_checker
  puts
  err.puts Usage.call(result)
  exit(1)
end