Class: TTY::Option::Parser::Environments
- Inherits:
-
Object
- Object
- TTY::Option::Parser::Environments
- Includes:
- ParamTypes
- Defined in:
- lib/tty/option/parser/environments.rb
Constant Summary collapse
- ENV_VAR_RE =
/([\p{Lu}_\-\d]+)=([^=]+)/.freeze
Constants included from ParamTypes
ParamTypes::ARGUMENT_PARAMETER, ParamTypes::ENV_VAR_PARAMETER, ParamTypes::KEYWORD_PARAMETER, ParamTypes::OPTION_PARAMETER
Instance Method Summary collapse
-
#initialize(environments, check_invalid_params: true, raise_on_parse_error: false) ⇒ Environments
constructor
Create a command line env variables parser.
-
#parse(argv, env) ⇒ Object
Read environment variable(s) from command line or ENV hash.
Methods included from ParamTypes
#argument?, #env_var?, #keyword?, #option?
Constructor Details
#initialize(environments, check_invalid_params: true, raise_on_parse_error: false) ⇒ Environments
Create a command line env variables parser
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tty/option/parser/environments.rb', line 25 def initialize(environments, check_invalid_params: true, raise_on_parse_error: false) @environments = environments @check_invalid_params = check_invalid_params @error_aggregator = ErrorAggregator.new(raise_on_parse_error: raise_on_parse_error) @required_check = RequiredCheck.new(@error_aggregator) @arity_check = ArityCheck.new(@error_aggregator) @pipeline = Pipeline.new(@error_aggregator) @parsed = {} @remaining = [] @names = {} @arities = Hash.new(0) @environments.each do |env_arg| @names[env_arg.name] = env_arg @arity_check << env_arg if env_arg.multiple? if env_arg.default? case env_arg.default when Proc assign_envvar(env_arg, env_arg.default.()) else assign_envvar(env_arg, env_arg.default) end elsif env_arg.required? @required_check << env_arg end end end |
Instance Method Details
#parse(argv, env) ⇒ Object
Read environment variable(s) from command line or ENV hash
62 63 64 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/tty/option/parser/environments.rb', line 62 def parse(argv, env) @argv = argv.dup @env = env loop do env_var, value = next_envvar if !env_var.nil? @required_check.delete(env_var) @arities[env_var.key] += 1 if block_given? yield(env_var, value) end assign_envvar(env_var, value) end break if @argv.empty? end @environments.each do |env_arg| if (value = env[env_arg.name]) @required_check.delete(env_arg) @arities[env_arg.key] += 1 assign_envvar(env_arg, value) end end @arity_check.(@arities) @required_check.() [@parsed, @remaining, @error_aggregator.errors] end |