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.

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.



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}



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.



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.

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