Class: Cuprum::Cli::Command

Inherits:
Cuprum::Command
  • Object
show all
Extended by:
Arguments::ClassMethods, Options::ClassMethods
Includes:
Metadata, Plumbum::Consumer, Plumbum::Parameters
Defined in:
lib/cuprum/cli/command.rb

Overview

Abstract base class for defining CLI commands.

Constant Summary

Constants included from Metadata

Metadata::FULL_NAME_FORMAT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Arguments::ClassMethods

argument, argument_value, argument_values, resolve_arguments

Methods included from Options::ClassMethods

option, option_value, option_values, resolve_options

Constructor Details

#initializeCommand

Returns a new instance of Command.



47
48
49
50
51
52
# File 'lib/cuprum/cli/command.rb', line 47

def initialize(**)
  super()

  @arguments = {}
  @options   = {}
end

Class Method Details

.argument(name, default: nil, description: nil, required: false, type: :string, variadic: false, **options) ⇒ Object

Defines an argument for the command class.

Parameters:

  • name (String, Symbol)

    the name of the argument.

  • default (Object, Proc) (defaults to: nil)

    the default value for the argument. If given and the value of the argument is nil, sets the argument value to the default value.

  • description (String) (defaults to: nil)

    a short, human-readable description of the argument.

  • required (true, false) (defaults to: false)

    if true, raises an exception if the argument is not provided to the command. Defaults to false.

  • type (Class, String, Symbol) (defaults to: :string)

    the expected type of the argument value as a Class or class name. If given, raises an exception if the argument value is not an instance of the type. Defaults to :string.

  • variadic (true, false) (defaults to: false)

    if true, the argument is variadic and represents an array of arguments provided to the command. Defaults to false.

  • options (Hash)

    additional options for defining the argument.

Options Hash (**options):

  • define_method (true, false)

    if true, defines a reader method for the argument. Defaults to false for boolean arguments and true for all other arguments.

  • define_predicate (true, false)

    if true, defines a predicate method for the argument, which returns true if the argument is not nil and not empty. Defaults to true for boolean arguments and false for all other arguments.

Raises:

  • (ArgumentError)

    if variadic is true and the command already defines a variadic argument.



23
24
25
26
27
28
# File 'lib/cuprum/cli/command.rb', line 23

def argument(argument_name, **)
  return super unless abstract?

  raise Cuprum::Cli::Metadata::AbstractCommandError,
    abstract_command_message("define argument :#{argument_name}")
end

.dependency(dependency_name) ⇒ Object



31
32
33
34
35
36
# File 'lib/cuprum/cli/command.rb', line 31

def dependency(dependency_name, **)
  return super unless abstract?

  raise Cuprum::Cli::Metadata::AbstractCommandError,
    abstract_command_message("add dependency :#{dependency_name}")
end

.option(option_name) ⇒ Object



39
40
41
42
43
44
# File 'lib/cuprum/cli/command.rb', line 39

def option(option_name, **)
  return super unless abstract?

  raise Cuprum::Cli::Metadata::AbstractCommandError,
    abstract_command_message("define option :#{option_name}")
end

Instance Method Details

#call(*arguments, **options) ⇒ Cuprum::Result #call(resolved_arguments:, resolved_options:) ⇒ Cuprum::Result

Overloads:

  • #call(*arguments, **options) ⇒ Cuprum::Result

    Resolves the parameters and calls the command.

    Parameters:

    • arguments (Array)

      arguments passed to the command.

    • options (Hash)

      options passed to the command.

    Returns:

    • (Cuprum::Result)

      the command result.

    Raises:

  • #call(resolved_arguments:, resolved_options:) ⇒ Cuprum::Result

    Calls the command with the given arguments and options.

    This variant assumes that the arguments and options have already been parsed and validated against the command's expected parameters.

    Parameters:

    • resolved_arguments (Hash)

      the arguments passed to the command, formatted as an Hash with the matching argument name as key and the argument value as the corresponding value.

    • resolved_options (Hash)

      the options passed to the command.

    Returns:

    • (Cuprum::Result)

      the command result.



83
84
85
86
87
88
89
90
# File 'lib/cuprum/cli/command.rb', line 83

def call(*, resolved_arguments: nil, resolved_options: nil, **)
  @arguments =
    resolved_arguments || self.class.resolve_arguments(*)
  @options   =
    resolved_options   || self.class.resolve_options(**)

  super()
end