Module: Cuprum::Cli::Arguments::ClassMethods

Included in:
Command
Defined in:
lib/cuprum/cli/arguments/class_methods.rb

Overview

Methods used to extend command class functionality for defining arguments.

Defined Under Namespace

Classes: Builder

Instance Method Summary collapse

Instance 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.



147
148
149
150
151
# File 'lib/cuprum/cli/arguments/class_methods.rb', line 147

def argument(name, define_method: nil, define_predicate: nil, **)
  handle_multiple_variadic_arguments(**)

  arguments_builder.call(name, define_method:, define_predicate:, **)
end

#argument_value(value) ⇒ void

This method returns an undefined value.

Appends a predefined argument value for the command.

Parameters:

  • value (Object)

    the argument value to append.



158
159
160
161
162
# File 'lib/cuprum/cli/arguments/class_methods.rb', line 158

def argument_value(value)
  defined_argument_values << value

  nil
end

#argument_valuesArray<Object>

Returns predefined argument values for the command.

Returns:

  • (Array<Object>)

    predefined argument values for the command.



165
# File 'lib/cuprum/cli/arguments/class_methods.rb', line 165

def argument_values = defined_argument_values

#argumentsArray<Cuprum::Cli::Argument> #arguments(name, default: nil, description: nil, required: false, type: :string, **options) ⇒ Object

Overloads:

  • #argumentsArray<Cuprum::Cli::Argument>

    The defined arguments for the command class.

    Returns:

  • #arguments(name, default: nil, description: nil, required: false, type: :string, **options) ⇒ Object

    Defines a variadic 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.

    • 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.

    • 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.



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/cuprum/cli/arguments/class_methods.rb', line 195

def arguments(name = nil, **)
  if name.nil?
    return defined_arguments unless defined_arguments.empty?

    return superclass.arguments if superclass.respond_to?(:arguments)

    return []
  end

  argument(name, **, variadic: true)
end

#resolve_arguments(*values) ⇒ Array

Validates the given argument values against the defined class arguments.

Also applies any default values from the defined arguments.

Parameters:

  • values (Array)

    the arguments values to resolve.

Returns:

  • (Array)

    the arguments values with applied defaults.

Raises:



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/cuprum/cli/arguments/class_methods.rb', line 219

def resolve_arguments(*values) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  values            = argument_values + values
  defined_arguments = arguments

  if defined_arguments.any?(&:variadic?)
    return VariadicArgumentsResolver.new(defined_arguments).call(*values)
  end

  if values.size > defined_arguments.size
    raise Cuprum::Cli::Arguments::ExtraArgumentsError,
      extra_arguments_message(values.size)
  end

  defined_arguments.each.with_index.to_h do |argument, index|
    [argument.name, argument.resolve(values[index])]
  end
end