tty-option 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,37 +4,34 @@ require_relative "result"
4
4
 
5
5
  module TTY
6
6
  module Option
7
+ # Responsible for parameter validation
8
+ #
9
+ # @api private
7
10
  module ParamValidation
8
11
  # Validate parameter value against validation rule
9
12
  #
10
13
  # @example
11
- # param = Parameter::Option.create(:foo, validate: '\d+')
12
- # ParamValidation[param, "12"] # => "12"
14
+ # param = TTY::Option::Parameter::Option.create(:foo, validate: "\d+")
15
+ # TTY::Option::ParamValidation[param, "12"] # => "12"
16
+ #
17
+ # @param [TTY::Option::Parameter] param
18
+ # the parameter with a validation rule
19
+ # @param [Object] value
20
+ # the value to validate
21
+ #
22
+ # @return [TTY::Option::Result]
13
23
  #
14
24
  # @api public
15
- def call(param, values)
16
- return Result.success(values) unless param.validate?
25
+ def call(param, value)
26
+ return Result.success(value) if !param.validate? || value.nil?
17
27
 
18
28
  errors = []
19
-
20
- result = Array(values).reduce([]) do |acc, value|
21
- valid = case param.validate
22
- when Proc
23
- param.validate.(value)
24
- when Regexp
25
- !param.validate.match(value.to_s).nil?
26
- end
27
-
28
- if valid
29
- acc << value
30
- else
31
- errors << TTY::Option::InvalidArgument.new(param, value)
32
- end
33
- acc
29
+ result = validate_object(param, value) do |error|
30
+ errors << error
34
31
  end
35
32
 
36
33
  if errors.empty?
37
- Result.success(result.size <= 1 ? result.first : result)
34
+ Result.success(result)
38
35
  else
39
36
  Result.failure(errors)
40
37
  end
@@ -43,6 +40,113 @@ module TTY
43
40
 
44
41
  alias [] call
45
42
  module_function :[]
43
+
44
+ # Validate an object
45
+ #
46
+ # @param [TTY::Option::Parameter] param
47
+ # the parameter with a validation rule
48
+ # @param [Object] value
49
+ # the value to validate
50
+ #
51
+ # @yield [TTY::Option::InvalidArgument]
52
+ #
53
+ # @return [Object, nil]
54
+ #
55
+ # @api private
56
+ def validate_object(param, value, &block)
57
+ case value
58
+ when Array
59
+ validate_array(param, value, &block)
60
+ when Hash
61
+ validate_hash(param, value, &block)
62
+ else
63
+ error = valid_or_error(param, value)
64
+ error ? block.(error) && return : value
65
+ end
66
+ end
67
+ module_function :validate_object
68
+ private_class_method :validate_object
69
+
70
+ # Validate array values
71
+ #
72
+ # @param [TTY::Option::Parameter] param
73
+ # the parameter with a validation rule
74
+ # @param [Object] values
75
+ # the values in an array to validate
76
+ #
77
+ # @yield [TTY::Option::InvalidArgument]
78
+ #
79
+ # @return [Array]
80
+ #
81
+ # @api private
82
+ def validate_array(param, values)
83
+ values.each_with_object([]) do |value, acc|
84
+ error = valid_or_error(param, value)
85
+ error ? yield(error) : acc << value
86
+ end
87
+ end
88
+ module_function :validate_array
89
+ private_class_method :validate_array
90
+
91
+ # Validate hash values
92
+ #
93
+ # @param [TTY::Option::Parameter] param
94
+ # the parameter with a validation rule
95
+ # @param [Object] values
96
+ # the values in a hash to validate
97
+ #
98
+ # @yield [TTY::Option::InvalidArgument]
99
+ #
100
+ # @return [Hash]
101
+ #
102
+ # @api private
103
+ def validate_hash(param, values)
104
+ values.each_with_object({}) do |value, acc|
105
+ error = valid_or_error(param, value)
106
+ error ? yield(error) : acc[value[0]] = value[1]
107
+ end
108
+ end
109
+ module_function :validate_hash
110
+ private_class_method :validate_hash
111
+
112
+ # Create an error for an invalid parameter value
113
+ #
114
+ # @param [TTY::Option::Parameter] param
115
+ # the parameter with a validation rule
116
+ # @param [Object] value
117
+ # the value to validate
118
+ #
119
+ # @return [TTY::Option::InvalidArgument, nil]
120
+ #
121
+ # @api private
122
+ def valid_or_error(param, value)
123
+ return if valid?(param, value)
124
+
125
+ TTY::Option::InvalidArgument.new(param, value)
126
+ end
127
+ module_function :valid_or_error
128
+ private_class_method :valid_or_error
129
+
130
+ # Check whether a parameter value is valid or not
131
+ #
132
+ # @param [TTY::Option::Parameter] param
133
+ # the parameter with a validation rule
134
+ # @param [Object] value
135
+ # the value to validate
136
+ #
137
+ # @return [Boolean]
138
+ #
139
+ # @api private
140
+ def valid?(param, value)
141
+ case param.validate
142
+ when Proc
143
+ param.validate.(value)
144
+ when Regexp
145
+ !param.validate.match(value.to_s).nil?
146
+ end
147
+ end
148
+ module_function :valid?
149
+ private_class_method :valid?
46
150
  end # ParamValidation
47
151
  end # Option
48
152
  end # TTY
@@ -274,7 +274,8 @@ module TTY
274
274
  value
275
275
  else
276
276
  raise ConfigurationError,
277
- "#{to_sym} '#{name}' permitted value needs to be an Array"
277
+ "#{to_sym} '#{name}' permitted value needs to be " \
278
+ "an Array or a Hash"
278
279
  end
279
280
  end
280
281
 
@@ -175,7 +175,12 @@ module TTY
175
175
  @remaining << long
176
176
  end
177
177
  elsif matching_options == 1
178
- value = long[opt.long_name.size..-1]
178
+ # option stuck together with the argument
179
+ if sep.nil? && rest.empty?
180
+ value = long[opt.long_name.size..-1]
181
+ else
182
+ value = rest
183
+ end
179
184
  else
180
185
  @error_aggregator.(AmbiguousOption.new("option '#{long}' is ambiguous"))
181
186
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  module Option
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end # Option
7
7
  end # TTY
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty-option
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-04 00:00:00.000000000 Z
11
+ date: 2023-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -100,6 +100,7 @@ metadata:
100
100
  changelog_uri: https://github.com/piotrmurach/tty-option/blob/master/CHANGELOG.md
101
101
  documentation_uri: https://www.rubydoc.info/gems/tty-option
102
102
  homepage_uri: https://ttytoolkit.org
103
+ rubygems_mfa_required: 'true'
103
104
  source_code_uri: https://github.com/piotrmurach/tty-option
104
105
  post_install_message:
105
106
  rdoc_options: []