Class: Cuprum::Cli::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/cli/registry.rb

Overview

Registers CLI commands by name.

Direct Known Subclasses

Integrations::Thor::Registry

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Class?

Returns the command registered with the given name, if any.

Returns:

  • (Class, nil)

    the command registered with the given name, if any.



9
10
11
12
13
# File 'lib/cuprum/cli/registry.rb', line 9

def [](name)
  tools.assertions.validate_name(name, as: 'full_name')

  registered_commands[name]
end

#commandsHash{String => Class}

Returns a copy of the commands registered with the registry.

Returns:

  • (Hash{String => Class})

    the registered commands.



18
19
20
# File 'lib/cuprum/cli/registry.rb', line 18

def commands
  registered_commands.dup.freeze
end

#register(command, **config) ⇒ self Also known as: add

Registers the command with the registry.

Parameters:

  • command (Class)

    the command class to register.

  • config (Hash)

    options for configuring the command.

Options Hash (**config):

  • arguments (Array)

    arguments to pass to the command on initialization.

  • description (String)

    the description for the command.

  • full_description (String)

    the full description for the command.

  • full_name (String)

    the name under which to register the command. Defaults to the value of command.full_name.

  • options (Hash)

    options to pass to the command on initialization.

Returns:

  • (self)

Raises:

  • (NameError)

    if a command is already registered with that name.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cuprum/cli/registry.rb', line 40

def register(command, **config)
  validate_command(command)

  name = config.fetch(:full_name, command.full_name)

  validate_name(name)

  command = build_command(command, **config) if any_present?(config)

  registered_commands[name] = command

  self
end