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
-
#argument(name, default: nil, description: nil, required: false, type: :string, variadic: false, **options) ⇒ Object
Defines an argument for the command class.
-
#argument_value(value) ⇒ void
Appends a predefined argument value for the command.
-
#argument_values ⇒ Array<Object>
Predefined argument values for the command.
- #arguments(name = nil) ⇒ Object
-
#resolve_arguments(*values) ⇒ Array
Validates the given argument values against the defined class arguments.
Instance Method Details
#argument(name, default: nil, description: nil, required: false, type: :string, variadic: false, **options) ⇒ Object
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.
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_values ⇒ Array<Object>
Returns predefined argument values for the command.
165 |
# File 'lib/cuprum/cli/arguments/class_methods.rb', line 165 def argument_values = defined_argument_values |
#arguments ⇒ Array<Cuprum::Cli::Argument> #arguments(name, default: nil, description: nil, required: false, type: :string, **options) ⇒ Object
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.
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, (values.size) end defined_arguments.each.with_index.to_h do |argument, index| [argument.name, argument.resolve(values[index])] end end |