Class: Cuprum::Cli::Arguments::ClassMethods::Builder

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

Overview

Helper class for defining command arguments.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_class:, defined_arguments:) ⇒ Builder

Returns a new instance of Builder.

Parameters:

  • the command class.

  • the arguments defined for the command.



15
16
17
18
# File 'lib/cuprum/cli/arguments/class_methods.rb', line 15

def initialize(command_class:, defined_arguments:)
  @command_class     = command_class
  @defined_arguments = defined_arguments
end

Instance Attribute Details

#command_classClass (readonly)

Returns the command class.

Returns:

  • the command class.



21
22
23
# File 'lib/cuprum/cli/arguments/class_methods.rb', line 21

def command_class
  @command_class
end

#defined_argumentsArray<Cuprum::Cli::Argument> (readonly)

Returns the arguments defined for the command.

Returns:

  • the arguments defined for the command.



25
26
27
# File 'lib/cuprum/cli/arguments/class_methods.rb', line 25

def defined_arguments
  @defined_arguments
end

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] 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] a short, human-readable description of the argument.

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

  • type [Class, String, Symbol] 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] 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.

Raises:

  • [ArgumentError] if variadic is true and the command already defines a variadic argument.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cuprum/cli/arguments/class_methods.rb', line 28

def call(name, define_method: nil, define_predicate: nil, **options)
  argument = Cuprum::Cli::Argument.new(name:, **options)

  defined_arguments << argument

  define_method    = (options[:type] != :boolean) if define_method.nil?
  define_predicate = (options[:type] == :boolean) if define_predicate.nil?

  define_method_for(argument)    if define_method
  define_predicate_for(argument) if define_predicate

  argument.name
end