Class: Cuprum::Cli::Option
- Inherits:
-
Data
- Object
- Data
- Cuprum::Cli::Option
- Defined in:
- lib/cuprum/cli/option.rb
Overview
Data object representing a command option.
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameter_name ⇒ Object
readonly
Returns the value of attribute parameter_name.
-
#required ⇒ Object
(also: #required?)
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#variadic ⇒ Object
(also: #variadic?)
readonly
Returns the value of attribute variadic.
Instance Method Summary collapse
-
#initialize(name:, aliases: [], default: nil, description: nil, parameter_name: nil, required: false, type: :string, variadic: false) ⇒ Option
constructor
A new instance of Option.
-
#resolve(value) ⇒ Object
Validates the value for the current option.
Constructor Details
#initialize(name:, aliases: [], default: nil, description: nil, parameter_name: nil, required: false, type: :string, variadic: false) ⇒ Option
Returns a new instance of Option.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cuprum/cli/option.rb', line 36 def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists name:, aliases: [], default: nil, description: nil, parameter_name: nil, required: false, type: :string, variadic: false ) name = name.to_sym aliases = Array(aliases).compact.map { |obj| obj.to_s.tr('_', '-') } required = required ? true : false type = type.to_sym if type.is_a?(String) variadic = variadic ? true : false super( aliases:, default:, description:, name:, parameter_name:, required:, type:, variadic: ) end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases
8 9 10 |
# File 'lib/cuprum/cli/option.rb', line 8 def aliases @aliases end |
#default ⇒ Object (readonly)
Returns the value of attribute default
8 9 10 |
# File 'lib/cuprum/cli/option.rb', line 8 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description
8 9 10 |
# File 'lib/cuprum/cli/option.rb', line 8 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name
8 9 10 |
# File 'lib/cuprum/cli/option.rb', line 8 def name @name end |
#parameter_name ⇒ Object (readonly)
Returns the value of attribute parameter_name
8 9 10 |
# File 'lib/cuprum/cli/option.rb', line 8 def parameter_name @parameter_name end |
#required ⇒ Object (readonly) Also known as: required?
Returns the value of attribute required
8 9 10 |
# File 'lib/cuprum/cli/option.rb', line 8 def required @required end |
#type ⇒ Object (readonly)
Returns the value of attribute type
8 9 10 |
# File 'lib/cuprum/cli/option.rb', line 8 def type @type end |
#variadic ⇒ Object (readonly) Also known as: variadic?
Returns the value of attribute variadic
8 9 10 |
# File 'lib/cuprum/cli/option.rb', line 8 def variadic @variadic end |
Instance Method Details
#resolve(value) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cuprum/cli/option.rb', line 79 def resolve(original_value) value = original_value value = default_value if blank?(value) value = value.to_s if value.is_a?(Symbol) return default_value_for_type if value.nil? && !required? return value if valid_option?(value) raise Cuprum::Cli::Options::InvalidOptionError, (original_value) end |