Class: Aspera::Cli::Option::Value
- Inherits:
-
Object
- Object
- Aspera::Cli::Option::Value
- Defined in:
- lib/aspera/cli/option/value.rb
Overview
Declared option: type, value, and where the value is stored.
Instance Attribute Summary collapse
-
#block ⇒ Object
[Proc, nil] Block to call for flag options.
-
#complete ⇒ Object
readonly
[Symbol, nil] Values proposed by shell completion, one of
Complete::KINDS. -
#deprecation ⇒ Object
readonly
Returns the value of attribute deprecation.
-
#group ⇒ Object
[String] Help section group name (set by Option::Registry).
-
#kind ⇒ Object
readonly
[Symbol] How values are converted: :flag, :boolean, :integer, :float, :enum, :enum_list, :string_list, :other.
-
#option ⇒ Object
readonly
[Symbol] Name of option.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#security ⇒ Object
readonly
[Symbol, nil] Most restrictive security level at which the option is available,
nil: all levels, seeSecurity. -
#sensitive ⇒ Object
readonly
Returns the value of attribute sensitive.
-
#short ⇒ Object
[String, nil] Short option char (set by Option::Registry).
-
#shorthand ⇒ Object
readonly
[String, nil] For a
Hashoption: key of aStringvalue. -
#source ⇒ Object
readonly
[Symbol, nil]
Option::Sourceof current value,nilif never set. -
#types ⇒ Object
readonly
[Array
, nil] List of allowed types, nilfor no validation. -
#values ⇒ Object
[Array] List of allowed values (Symbols and specific values).
Instance Method Summary collapse
-
#allowed_info ⇒ String?
Allowed values, or expected types when not plain
String, e.g. -
#assign_value(value, source:, warn_deprecation: true, merge: true, final: false, configured: false) ⇒ nil
Assign value to option.
-
#check_security(value) ⇒ Object
Check that the option, and the sub-options present in a
Hashvalue (x-securityin schema), are available at current security level. -
#clear ⇒ nil
Reset stored value to nil.
-
#description ⇒ String
Description of the option: explicit one, or first line of schema description.
-
#flag? ⇒ Boolean
trueif option takes no value. -
#initialize(option:, description: nil, allowed: Type::STRING, on_set: nil, shorthand: nil, convert: nil, deprecation: nil, schema: nil, complete: nil, security: nil) ⇒ Value
constructor
allowed: -nilNo validation, so just a string -ClassThe single allowed Class -Array<Class>Multiple allowed classes -Array<Symbol>List of allowed values. -
#schema_request?(value) ⇒ Boolean
trueif value asks for the schema of option. -
#validate_complete(value) ⇒ Object
Validate the value of the option once all sources are applied:
requiredof the schema is enforced. -
#value(log: true) ⇒ Object
Get current option value.
Constructor Details
#initialize(option:, description: nil, allowed: Type::STRING, on_set: nil, shorthand: nil, convert: nil, deprecation: nil, schema: nil, complete: nil, security: nil) ⇒ Value
allowed:
nilNo validation, so just a stringClassThe single allowed ClassArray<Class>Multiple allowed classesArray<Symbol>List of allowed values
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/aspera/cli/option/value.rb', line 65 def initialize(option:, description: nil, allowed: Type::STRING, on_set: nil, shorthand: nil, convert: nil, deprecation: nil, schema: nil, complete: nil, security: nil) Log.log.trace1 { "option: #{option}, allowed: #{allowed}" } @option = option @description = description @group = nil @short = nil @block = nil # by default passwords and secrets are sensitive, else specify when declaring the option @sensitive = Secret::Hider.instance.secret?(@option, '') @deprecation = Deprecation.create(deprecation) @schema = schema @shorthand = shorthand Aspera.assert(convert.nil? || convert.respond_to?(:call)) { "#{@option}: convert must respond to call" } @convert = convert Aspera.assert_values(complete, Complete::KINDS + [nil]) { "#{@option}: complete" } @complete = complete Aspera.assert_values(security, Security::LEVELS + [nil]) { "#{@option}: security" } @security = security @source = nil @value = nil Aspera.assert(on_set.nil? || on_set.respond_to?(:call)) { "#{@option}: on_set callback must respond to call" } @on_set = on_set @types = nil @values = nil @kind = :other allowed = infer_allowed_from_schema(schema, allowed) if schema apply_allowed(allowed) unless allowed.nil? end |
Instance Attribute Details
#block ⇒ Object
[Proc, nil] Block to call for flag options
48 49 50 |
# File 'lib/aspera/cli/option/value.rb', line 48 def block @block end |
#complete ⇒ Object (readonly)
[Symbol, nil] Values proposed by shell completion, one of Complete::KINDS
40 41 42 |
# File 'lib/aspera/cli/option/value.rb', line 40 def complete @complete end |
#deprecation ⇒ Object (readonly)
Returns the value of attribute deprecation.
34 35 36 |
# File 'lib/aspera/cli/option/value.rb', line 34 def deprecation @deprecation end |
#group ⇒ Object
[String] Help section group name (set by Option::Registry)
44 45 46 |
# File 'lib/aspera/cli/option/value.rb', line 44 def group @group end |
#kind ⇒ Object (readonly)
[Symbol] How values are converted: :flag, :boolean, :integer, :float, :enum, :enum_list, :string_list, :other
31 32 33 |
# File 'lib/aspera/cli/option/value.rb', line 31 def kind @kind end |
#option ⇒ Object (readonly)
[Symbol] Name of option
27 28 29 |
# File 'lib/aspera/cli/option/value.rb', line 27 def option @option end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
34 35 36 |
# File 'lib/aspera/cli/option/value.rb', line 34 def schema @schema end |
#security ⇒ Object (readonly)
[Symbol, nil] Most restrictive security level at which the option is available, nil: all levels, see Security
36 37 38 |
# File 'lib/aspera/cli/option/value.rb', line 36 def security @security end |
#sensitive ⇒ Object (readonly)
Returns the value of attribute sensitive.
34 35 36 |
# File 'lib/aspera/cli/option/value.rb', line 34 def sensitive @sensitive end |
#short ⇒ Object
[String, nil] Short option char (set by Option::Registry)
46 47 48 |
# File 'lib/aspera/cli/option/value.rb', line 46 def short @short end |
#shorthand ⇒ Object (readonly)
[String, nil] For a Hash option: key of a String value
38 39 40 |
# File 'lib/aspera/cli/option/value.rb', line 38 def shorthand @shorthand end |
#source ⇒ Object (readonly)
[Symbol, nil] Option::Source of current value, nil if never set
33 34 35 |
# File 'lib/aspera/cli/option/value.rb', line 33 def source @source end |
#types ⇒ Object (readonly)
[Arraynil for no validation
29 30 31 |
# File 'lib/aspera/cli/option/value.rb', line 29 def types @types end |
#values ⇒ Object
[Array] List of allowed values (Symbols and specific values)
42 43 44 |
# File 'lib/aspera/cli/option/value.rb', line 42 def values @values end |
Instance Method Details
#allowed_info ⇒ String?
Returns allowed values, or expected types when not plain String, e.g. Hash|String.
104 105 106 107 108 109 |
# File 'lib/aspera/cli/option/value.rb', line 104 def allowed_info return @values.join('|') if @values&.any? types = @types&.reject { |t| t.eql?(NilClass) } return if types.nil? || types.empty? || types.eql?(Type::STRING) types.map(&:name).join('|') end |
#assign_value(value, source:, warn_deprecation: true, merge: true, final: false, configured: false) ⇒ nil
Assign value to option.
Value can be a String, then evaluated with ExtendedValue, or directly a value.
Hash and Array values are merged with current value.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/aspera/cli/option/value.rb', line 144 def assign_value(value, source:, warn_deprecation: true, merge: true, final: false, configured: false) # Value from a source with lower priority than current value: only fills containers lower = !@source.nil? && Option::Source.priority(source) < Option::Source.priority(@source) # Final value (set by the program for the whole run): a lower source is ignored, even for a container if lower && @final Log.log.debug { "#{source}: #{@option}: ignored, final value set from #{@source}" } return end if lower && ![Hash, Array].include?(@types&.first) Log.log.debug { "#{source}: #{@option}: ignored, already set from #{@source}" } return end Aspera.assert(!@deprecation, type: :warn) { "Option #{@option} is #{@deprecation}" } if warn_deprecation # Values set by the program, the environment or the configuration are not restricted: those from command line, a preset given by the client, interactive input are restricted = restricted_source?(source) && !configured Security.instance.check_level(@security, "option #{@option}") if restricted if schema_request?(value) return if lower raise SchemaRequest.new(:option, @option, @schema) unless @schema.nil? # Schema depends on command: kept as-is, raised by Parser#get_option(schema:) store(value, source) return end new_value = coerce(ExtendedValue.instance.evaluate(value, context: "option: #{@option}", allowed: @types)) (new_value) if restricted # Set by the one who starts the process: a secret (e.g. in a `Hash`) of the environment can be sent to a URL of the environment, see `Security#check_credentials_in` Security.instance.add_configured(new_value) if source.eql?(:env) new_value = {@shorthand => new_value} if @shorthand && new_value.is_a?(String) declare_secrets(new_value) Log.log.trace1 { "#{source}: #{@option} <- (#{new_value.class})#{Secret::Hider.instance.hidden_copy(new_value)}" } Aspera.assert_type(new_value, *@types, type: BadArgument) { "Option #{@option}" } if @types new_value = @convert.call(new_value) if @convert && new_value.is_a?(Hash) if merge && (new_value.is_a?(Hash) || new_value.is_a?(Array)) current_value = @value # An `Array` default value is replaced, not appended to mergeable = current_value.is_a?(new_value.is_a?(Hash) ? Hash : Array) && !current_value.empty? && !(new_value.is_a?(Array) && [source, @source].include?(:default)) if lower # Current value has priority: merge new value under it, unless explicitly emptied return unless mergeable new_value, current_value = current_value, new_value source = @source end new_value = new_value.is_a?(Hash) ? current_value.deep_merge(new_value) : current_value + new_value if mergeable end # A value set by the program is not checked, e.g. `--out.img` sets option `image` with a value checked as part of option `out` validate_schema(new_value, partial: true) unless source.eql?(:code) store(new_value, source) @final = true if final nil end |
#check_security(value) ⇒ Object
Check that the option, and the sub-options present in a Hash value (x-security in schema), are available at current security level.
Used for a value that is not assigned yet, e.g. to store in a preset.
207 208 209 210 |
# File 'lib/aspera/cli/option/value.rb', line 207 def check_security(value) Security.instance.check_level(@security, "option #{@option}") (value) end |
#clear ⇒ nil
Reset stored value to nil
120 121 122 |
# File 'lib/aspera/cli/option/value.rb', line 120 def clear store(nil, nil) end |
#description ⇒ String
Returns description of the option: explicit one, or first line of schema description.
95 96 97 98 99 100 101 |
# File 'lib/aspera/cli/option/value.rb', line 95 def description return @description unless @description.nil? return if @schema.nil? schema_node = Schema::Registry.instance.reader(@schema).current first_line = (schema_node['title'] || schema_node['description'].to_s).lines.first.to_s.strip first_line.end_with?('.') ? first_line[0..-2] : first_line end |
#flag? ⇒ Boolean
Returns true if option takes no value.
112 |
# File 'lib/aspera/cli/option/value.rb', line 112 def flag? = @kind.eql?(:flag) |
#schema_request?(value) ⇒ Boolean
Returns true if value asks for the schema of option.
116 |
# File 'lib/aspera/cli/option/value.rb', line 116 def schema_request?(value) = value.eql?(SchemaRequest::KEYWORD) && @types&.include?(Hash) |
#validate_complete(value) ⇒ Object
Validate the value of the option once all sources are applied: required of the schema is enforced.
199 200 201 |
# File 'lib/aspera/cli/option/value.rb', line 199 def validate_complete(value) validate_schema(value, partial: false) end |