Method: Thor::Execution#get_context_value

Defined in:
lib/thor/execution.rb

#get_context_value(key, with_source: false) ⇒ Object, Array<(Object, Symbol?, (String | Symbol)?>)

Get the value for a key from the “context”, which is the hierarchy of Thor instance class options, Thor config values and ENV variables.

Parameters:

  • key (Symbol)

    The key to get the value for.

  • with_source: (Boolean) (defaults to: false)

    When true, returns where the value was found as well (see below).

Returns:

  • (Object)

    When with_source: is false, just returns the value that was found, or nil if none was.

  • (Array<(Object, Symbol?, (String | Symbol)?>))

    When with_source: is true returns an Array triple:

    1. The value that was found, or nil if none was.

    2. A symbol indicating where the value was found:

      1. :thor_instance_options The #options hash of the #thor_instance. You will only see this result if the Thor instance successfully constructed and became available to the execution.

      2. :thor_config The #thor_config hash.

      3. :env The ENV.

      If the value isn’t found, this will be nil.

    3. The key used to get the value from the source hash-like. This is only really important when it came from the ENV.

      If the value is not found, this will be nil.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/thor/execution.rb', line 199

def get_context_value key, with_source: false
  # 1.  First stop is the Thor instance's options (if we have a Thor instance)
  if thor_instance && thor_instance.options.key?( key )
    if with_source
      return [thor_instance.options[key], :thor_instance_options, key]
    else
      return thor_instance.options[key]
    end
  end
  
  # 2.  Next, check the config that was handed to `.exec!`
  if thor_config.key? key
    if with_source
      return [thor_config[key], :thor_config, key]
    else
      return thor_config[key]
    end
  end
  
  # 3. Last, check the ENV (returns `nil` if nothing found)
  get_from_env key, with_source: with_source
end