Class: Cuprum::Cli::Options::ClassMethods::Builder

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

Overview

Helper class for defining command options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_class:, defined_options:) ⇒ Builder

Returns a new instance of Builder.

Parameters:

  • command_class (Class)

    the command class.

  • defined_options (Hash{Symbol => Cuprum::Cli::Option})

    the options defined for the command.



16
17
18
19
# File 'lib/cuprum/cli/options/class_methods.rb', line 16

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

Instance Attribute Details

#command_classClass (readonly)

Returns the command class.

Returns:

  • (Class)

    the command class.



22
23
24
# File 'lib/cuprum/cli/options/class_methods.rb', line 22

def command_class
  @command_class
end

#defined_optionsHash{Symbol => Cuprum::Cli::Option} (readonly)

Returns the options defined for the command.

Returns:



26
27
28
# File 'lib/cuprum/cli/options/class_methods.rb', line 26

def defined_options
  @defined_options
end

Instance Method Details

#option(name, aliases: [], default: nil, description: nil, required: false, type: :string, **options) ⇒ Object

Defines an option for the command class.

Parameters:

  • name (String, Symbol)

    the name of the option.

  • aliases (Array<String, Symbol>) (defaults to: [])

    aliases for the option when parsing options from the command line.

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

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

  • description (String) (defaults to: nil)

    a short, human-readable description of the option.

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

    if true, raises an exception if the option is not provided to the command.

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

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

  • options (Hash)

    additional options for defining the option.

Options Hash (**options):

  • define_method (true, false)

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

  • define_predicate (true, false)

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



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

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

  defined_options[option.name] = option

  boolean_option = options[:type] == :boolean && !options[:variadic]

  define_method    = !boolean_option if define_method.nil?
  define_predicate = boolean_option  if define_predicate.nil?

  define_method_for(option)    if define_method
  define_predicate_for(option) if define_predicate

  option.name
end