A command-line utility powered by Cuprum that provides tools and utilities for defining command-line tools.
Registers CLI commands by name and adds as Thor tasks.
#[](name) => Class, nil
#commands => Hash{String => Class}
Returns a copy of the commands registered with the registry.
#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.
Register a command
registry.register(ExampleCommand)
registry.commands.key?('example')
#=> trueRegister 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')
#=> trueRegister 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')
#=> trueBack to Documentation | Reference | Cuprum | Cuprum::Cli | Cuprum::Cli::Integrations | Cuprum::Cli::Integrations::Thor