class UserChoices::EnvironmentSource

Describe the environment as a source of choices.

Public Instance Methods

mapping(map) click to toggle source

Some environment variables have names you don’t like. For example, $HOME might be annoying because of the uppercase. Also, if most of your program’s environment variables have some prefix (see #with_prefix) but you also want to use $HOME, you need a way to do that. You can satisfy both desires with

EnvironmentSource.new.with_prefix("my_").mapping(:home => "HOME")
# File lib/user-choices/sources.rb, line 139
def mapping(map)
  @external_names.merge!(map)
  self
end
with_prefix(prefix) click to toggle source

Environment variables beginning with prefix (a string) are considered to be user choices relevant to this script. Everything after the prefix names a choice (that is, a symbol). Dashes are converted to underscores. Examples:

  • Environment variable prefix-my-choice with prefix "prefix-" is choice <tt>:my_choice.

  • Environment variable PREFIX_FOO with prefix "PREFIX_" is choice <tt>:FOO

If you want an array of strings, separate the values by commas: ENV_VAR=a,b,c There’s currently no way to escape a comma and no cleverness about quotes or whitespace.

# File lib/user-choices/sources.rb, line 122
def with_prefix(prefix)
  matches = ENV.collect do | env_var, ignored_value |
    if /^#{prefix}(.+)/ =~ env_var
      [$1.to_inputable_sym, env_var]
    end
  end
  @external_names.merge!(Hash[*matches.compact.flatten])
  self
end