Cuprum::Cli

A command-line utility powered by Cuprum that provides tools and utilities for defining command-line tools.

Class: Cuprum::Cli::Registry

Parent Namespace
Cuprum::Cli
Defined In
lib/cuprum/cli/registry.rb

Table Of Contents

Overview

Registers CLI commands by name.

Back To Top

Direct Subclasses

Cuprum::Cli::Integrations::Thor::Registry

Back To Top

Instance Methods

#[](name) => Class, nil

Returns

#commands => Hash{String => Class}

Returns a copy of the commands registered with the registry.

Returns

#register(*commands, **config, &block) => self

Also known as: add

Registers the commands with the registry.

If given a block, transforms the command using the block. This can be used to customize the commands, such as applying a custom prefix or configuration options. If the block instead returns nil, that command will not be registered.

Examples

Register a command

registry.register(ExampleCommand)

registry.commands.key?('example')
#=> true

Register a command with configuration options

registry.register ExampleCommand,
  full_name: 'scope:custom_command',
  options:   { option: 'value' }

registry.commands.key?('example')
#=> false
registry.commands.key?('scope:custom_command')
#=> true
registry.commands['scope:example_command'].option_values
#=> { option: 'value' }

Register multiple commands with configuration options

registry.register FirstCommand,
  SecondCommand,
  ThirdCommand,
  options: { shared: 'value' }

registry.commands.key?('first')
#=> true
registry.commands.key?('second')
#=> true
registry.commands.key?('third')
#=> true
registry.commands['first'].option_values
#=> { shared: 'value' }

Register multiple commands and configure with a block

registry.register(FirstCommand, SecondCommand, ThirdCommand) do |command|
  full_name = "scope:#{command.full_name}"

  command.with_full_name(full_name)
end

registry.commands.key?('scope:first')
#=> true
registry.commands.key?('scope:second')
#=> true
registry.commands.key?('scope:third')
#=> true

Register multiple commands and filter with a block

commands = [OneCommand, TwoCommand, ThreeCommand]

registry.register(commands) do |command|
  next unless command.full_name.start_with?('t')

  command
end

registry.commands.key?('one')
#=> false
registry.commands.key?('two')
#=> true
registry.commands.key?('three')
#=> true

Parameters

Options Hash (config)

Yield Parameters

Yield Returns

Returns

Raises

Back To Top


Back to Documentation | Reference | Cuprum | Cuprum::Cli