Module: Cuprum::Cli::Options::ClassMethods

Included in:
Command, Commands::File::GenerateFile
Defined in:
lib/cuprum/cli/options/class_methods.rb

Overview

Methods used to extend command class functionality for defining options.

Defined Under Namespace

Classes: Builder

Instance Method Summary collapse

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.



87
88
89
90
91
92
# File 'lib/cuprum/cli/options/class_methods.rb', line 87

def option(name, define_method: nil, define_predicate: nil, **)
  handle_multiple_variadic_options(**)

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

#option_value(option, value) ⇒ void

This method returns an undefined value.

Assigns a predefined option value for the command.

Parameters:

  • option (String, Symbol)

    the option to set.

  • value (Object)

    the option value to append.



100
101
102
103
104
# File 'lib/cuprum/cli/options/class_methods.rb', line 100

def option_value(option, value)
  defined_option_values[option.to_sym] = value

  nil
end

#option_valuesHash{Symbol => Object}

Returns predefined option values for the command.

Returns:

  • (Hash{Symbol => Object})

    predefined option values for the command.



107
# File 'lib/cuprum/cli/options/class_methods.rb', line 107

def option_values = inherited_option_values.merge(defined_option_values)

#optionsHash{Symbol => Cuprum::Cli::Option}

The defined options, including options defined on ancestor classes.

Returns:



112
113
114
115
116
117
118
119
# File 'lib/cuprum/cli/options/class_methods.rb', line 112

def options
  ancestors.reduce({}) do |hsh, ancestor|
    return hsh if     ancestor == Cuprum::Cli::Command
    next   hsh unless ancestor.respond_to?(:defined_options, true)

    hsh.merge(ancestor.defined_options)
  end
end

#resolve_options(**values) ⇒ Hash

Validates the given option values against the defined class options.

Also applies any default values from the defined options.

Parameters:

  • values (Hash)

    the option values to resolve.

Returns:

  • (Hash)

    the option values with applied defaults.

Raises:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cuprum/cli/options/class_methods.rb', line 133

def resolve_options(**values)
  values                  = option_values.merge(values)
  defined_options         = options
  values, unknown_options = resolve_variadic_values(values)

  unless unknown_options.empty?
    raise Cuprum::Cli::Options::UnknownOptionError,
      unknown_options_message(defined_options:, unknown_options:)
  end

  defined_options.to_h do |key, option|
    [key, option.resolve(values[key])]
  end
end