cri 2.9.0 → 2.9.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5285c76db1bf769830b9258e04e041ae4ce35110
4
- data.tar.gz: a4a34c4114ef34eb00deb31b62a60e7a39111086
3
+ metadata.gz: a4071cb78d426c77cba1c810c77e618098fd0ff5
4
+ data.tar.gz: d1d4bc176390cbbceab6b6f79ce12c907e27ae37
5
5
  SHA512:
6
- metadata.gz: 0546e56bded0584b797f35771344ef9057f903322b61e2fa94d17471250bcb92bda6cc77f7a4086526c9acd45e04221ffc773f1b17d8ff37d1add1d36fcce661
7
- data.tar.gz: 9c94286223b03f5bd3621ef51659a3239e70ef130aa4874156cf66db2650e527c15c74e40729706e83e10a466bdab12623cbc8ab0daf9ad136ff58d5fcfd3c4f
6
+ metadata.gz: f5a100be8c1fab79a67893e7e8ee63eccb3258159526fb9624292d8d713e7a4ed05b21c7074056c4eadb71106044a006ff931a2aaec14749c33acf94f844733f
7
+ data.tar.gz: ae2e7d5e146c463344c8a445977a949b0f676a88bcc993a663d5d6add5083fee76bf2015a8a10e7d5c77b4b46e158fa4976bdcb99307d7115e4578e97efba3f2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cri (2.9.0)
4
+ cri (2.9.1)
5
5
  colored (~> 1.2)
6
6
 
7
7
  GEM
data/NEWS.md CHANGED
@@ -1,6 +1,12 @@
1
1
  Cri News
2
2
  ========
3
3
 
4
+ ## 2.9.1
5
+
6
+ Fixes:
7
+
8
+ * Made default values be always returned, even when not explicitly specified (#57, #58)
9
+
4
10
  ## 2.9.0
5
11
 
6
12
  Features:
@@ -127,8 +127,8 @@ module Cri
127
127
  raise ArgumentError, 'short and long options cannot both be nil'
128
128
  end
129
129
 
130
- if default && requiredness != :optional
131
- raise ArgumentError, "a default value cannot be specified for options with #{requiredness} values"
130
+ if default && requiredness == :forbidden
131
+ raise ArgumentError, 'a default value cannot be specified for flag options'
132
132
  end
133
133
 
134
134
  @command.option_definitions << {
@@ -173,6 +173,9 @@ module Cri
173
173
  add_argument(e)
174
174
  end
175
175
  end
176
+
177
+ add_defaults
178
+
176
179
  self
177
180
  ensure
178
181
  @running = false
@@ -180,6 +183,10 @@ module Cri
180
183
 
181
184
  private
182
185
 
186
+ def add_defaults
187
+ @definitions.each { |d| add_default_option(d) }
188
+ end
189
+
183
190
  def handle_dashdash(e)
184
191
  add_argument(e)
185
192
  @no_more_options = true
@@ -252,7 +259,8 @@ module Cri
252
259
  end
253
260
 
254
261
  def add_option(definition, value)
255
- key = (definition[:long] || definition[:short]).to_sym
262
+ key = key_for(definition)
263
+
256
264
  if definition[:multiple]
257
265
  options[key] ||= []
258
266
  options[key] << value
@@ -263,6 +271,25 @@ module Cri
263
271
  delegate.option_added(key, value, self) unless delegate.nil?
264
272
  end
265
273
 
274
+ def add_default_option(definition)
275
+ key = key_for(definition)
276
+ return if options.key?(key)
277
+
278
+ value = definition[:default]
279
+ return unless value
280
+
281
+ if definition[:multiple]
282
+ options[key] ||= []
283
+ options[key] << value
284
+ else
285
+ options[key] = value
286
+ end
287
+ end
288
+
289
+ def key_for(definition)
290
+ (definition[:long] || definition[:short]).to_sym
291
+ end
292
+
266
293
  def add_argument(value)
267
294
  @raw_arguments << value
268
295
 
data/lib/cri/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Cri
2
2
  # The current Cri version.
3
- VERSION = '2.9.0'.freeze
3
+ VERSION = '2.9.1'.freeze
4
4
  end
@@ -161,15 +161,12 @@ module Cri
161
161
  end
162
162
  end
163
163
 
164
- def test_default_value_errors_when_requiredness_is_required
164
+ def test_default_value_with_equiredness_is_required
165
165
  dsl = Cri::CommandDSL.new
166
166
 
167
- err = assert_raises ArgumentError do
168
- dsl.instance_eval do
169
- required 'a', 'animal', 'Specify animal', default: 'giraffe'
170
- end
167
+ dsl.instance_eval do
168
+ required 'a', 'animal', 'Specify animal', default: 'giraffe'
171
169
  end
172
- assert_equal('a default value cannot be specified for options with required values', err.message)
173
170
  end
174
171
 
175
172
  def test_default_value_errors_when_requiredness_is_forbidden
@@ -180,7 +177,7 @@ module Cri
180
177
  flag 'a', 'animal', 'Allow animal', default: 'giraffe'
181
178
  end
182
179
  end
183
- assert_equal('a default value cannot be specified for options with forbidden values', err.message)
180
+ assert_equal('a default value cannot be specified for flag options', err.message)
184
181
  end
185
182
 
186
183
  def test_subcommand
@@ -282,6 +282,18 @@ module Cri
282
282
  assert_equal(3, parser.options[:verbose].size)
283
283
  end
284
284
 
285
+ def test_parse_with_default_required_unspecified
286
+ input = %w[foo]
287
+ definitions = [
288
+ { long: 'animal', short: 'a', argument: :required, default: 'donkey' },
289
+ ]
290
+
291
+ parser = Cri::OptionParser.parse(input, definitions)
292
+
293
+ assert_equal({ animal: 'donkey' }, parser.options)
294
+ assert_equal(['foo'], parser.arguments)
295
+ end
296
+
285
297
  def test_parse_with_default_required_no_value
286
298
  input = %w[foo -a]
287
299
  definitions = [
@@ -305,6 +317,18 @@ module Cri
305
317
  assert_equal(['foo'], parser.arguments)
306
318
  end
307
319
 
320
+ def test_parse_with_default_optional_unspecified
321
+ input = %w[foo]
322
+ definitions = [
323
+ { long: 'animal', short: 'a', argument: :optional, default: 'donkey' },
324
+ ]
325
+
326
+ parser = Cri::OptionParser.parse(input, definitions)
327
+
328
+ assert_equal({ animal: 'donkey' }, parser.options)
329
+ assert_equal(['foo'], parser.arguments)
330
+ end
331
+
308
332
  def test_parse_with_default_optional_no_value
309
333
  input = %w[foo -a]
310
334
  definitions = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cri
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.0
4
+ version: 2.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Defreyne