cri 2.15.6 → 2.15.7

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
  SHA256:
3
- metadata.gz: 79a5265f39985bdcfc272fda054b0c9351be2e249d14ce8dd85d3cbff3964479
4
- data.tar.gz: 9dcbb3ab7addc6dda23d1419370882401b91747c754e6a39d7777bbfc8e6c354
3
+ metadata.gz: 961c45fae4395d1aad6d4f3d79ccf4b00c32bbd38f816d52df500aea7c782da5
4
+ data.tar.gz: 7917db80f32845b87f5cc6942623a1ee34e5d83058cc526100afc07ea412b246
5
5
  SHA512:
6
- metadata.gz: 2e5fd6d1a6c496fb1887e99c02255f438b9fa6734180bb2d09d3dbb349f30136ee16007195c2848bdb26d797f38c6d22473beaa06bf7bdc7d86734619955136d
7
- data.tar.gz: c02067bfb085935f46cf8b35c37862a3eac282c1dfe2eb3bc6c6162f1370d4c5bfa390a2d8a80fc536e7207a5031e225c2e7c7a3bdf3ae8efa329c561402f8b2
6
+ metadata.gz: 48b820bcb56c95e6ff4594db88a814c72b5c10868e971a03236d47d4826af789228e9f38e6b7d926450bbb0700aac84d575b4cd613ad5a20f8adfcfa5f220606
7
+ data.tar.gz: f84359f5f263d1ab38400a8b4667e8b47c2cf0b9a8ef883e8896e7c86a39b5ecc874e146f215a855c116d1863e68e62f29164c50a61f99e00ec07938ca85aaee
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ source 'https://rubygems.org'
5
5
  gemspec
6
6
 
7
7
  gem 'coveralls'
8
+ gem 'm', '~> 1.5'
8
9
  gem 'minitest'
9
10
  gem 'rake'
10
11
  gem 'rubocop'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cri (2.15.6)
4
+ cri (2.15.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -16,20 +16,24 @@ GEM
16
16
  docile (1.3.1)
17
17
  jaro_winkler (1.5.2)
18
18
  json (2.2.0)
19
+ m (1.5.1)
20
+ method_source (>= 0.6.7)
21
+ rake (>= 0.9.2.2)
22
+ method_source (0.9.2)
19
23
  minitest (5.11.3)
20
24
  parallel (1.17.0)
21
25
  parser (2.6.3.0)
22
26
  ast (~> 2.4.0)
23
27
  rainbow (3.0.0)
24
28
  rake (12.3.2)
25
- rubocop (0.69.0)
29
+ rubocop (0.70.0)
26
30
  jaro_winkler (~> 1.5.1)
27
31
  parallel (~> 1.10)
28
32
  parser (>= 2.6)
29
33
  rainbow (>= 2.2.2, < 4.0)
30
34
  ruby-progressbar (~> 1.7)
31
35
  unicode-display_width (>= 1.4.0, < 1.7)
32
- ruby-progressbar (1.10.0)
36
+ ruby-progressbar (1.10.1)
33
37
  simplecov (0.16.1)
34
38
  docile (~> 1.1)
35
39
  json (>= 1.8, < 3)
@@ -48,6 +52,7 @@ PLATFORMS
48
52
  DEPENDENCIES
49
53
  coveralls
50
54
  cri!
55
+ m (~> 1.5)
51
56
  minitest
52
57
  rake
53
58
  rubocop
data/NEWS.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Cri News
2
2
 
3
+ ## 2.15.7
4
+
5
+ Fixes:
6
+
7
+ * Options with a forbidden argument now default to false, rather than nil (#94, #96)
8
+
3
9
  ## 2.15.6
4
10
 
5
11
  Fixes:
data/README.md CHANGED
@@ -14,6 +14,13 @@ nested commands.
14
14
 
15
15
  Cri requires Ruby 2.3 or newer.
16
16
 
17
+ ## Compatibility policy
18
+
19
+ Cri is guaranteed to be supported on any [officially supported Ruby version](https://www.ruby-lang.org/en/downloads/branches/), as well as the version of Ruby that comes by default on
20
+
21
+ * the last two [Ubuntu LTS releases](https://wiki.ubuntu.com/Releases)
22
+ * the last two major [macOS releases](https://en.wikipedia.org/wiki/MacOS_version_history)
23
+
17
24
  ## Usage
18
25
 
19
26
  The central concept in Cri is the _command_, which has option definitions as
@@ -346,6 +346,16 @@ module Cri
346
346
  local_opts = parser.options
347
347
  global_opts = parent_opts.merge(parser.options)
348
348
 
349
+ # Set defaults
350
+ all_opt_defns.each do |opt_defn|
351
+ key = (opt_defn.long || opt_defn.short).to_sym
352
+
353
+ next if opt_defn.default.nil?
354
+ next if global_opts.key?(key)
355
+
356
+ global_opts[key] = opt_defn.default
357
+ end
358
+
349
359
  # Handle options
350
360
  handle_options(local_opts)
351
361
  args = handle_errors_while { parser.gen_argument_list }
@@ -359,6 +369,10 @@ module Cri
359
369
  block.call(global_opts, args, self)
360
370
  end
361
371
 
372
+ def all_opt_defns
373
+ supercommand ? supercommand.all_opt_defns.merge(option_definitions) : option_definitions
374
+ end
375
+
362
376
  # @return [String] The help text for this command
363
377
  #
364
378
  # @option params [Boolean] :verbose true if the help output should be
@@ -31,6 +31,8 @@ module Cri
31
31
  if @default && @argument == :forbidden
32
32
  raise ArgumentError, 'a default value cannot be specified for flag options'
33
33
  end
34
+
35
+ @default = false if @argument == :forbidden
34
36
  end
35
37
 
36
38
  def to_h
@@ -118,8 +118,6 @@ module Cri
118
118
  end
119
119
  end
120
120
 
121
- add_defaults
122
-
123
121
  self
124
122
  ensure
125
123
  @running = false
@@ -133,10 +131,6 @@ module Cri
133
131
 
134
132
  private
135
133
 
136
- def add_defaults
137
- @option_defns.each { |d| add_default_option(d) }
138
- end
139
-
140
134
  def handle_dashdash(elem)
141
135
  add_argument(elem)
142
136
  @no_more_options = true
@@ -223,16 +217,6 @@ module Cri
223
217
  delegate&.option_added(key, value, self)
224
218
  end
225
219
 
226
- def add_default_option(option_defn)
227
- key = key_for(option_defn)
228
- return if options.key?(key)
229
-
230
- value = option_defn.default
231
- return unless value
232
-
233
- add_option(option_defn, value, transform: false)
234
- end
235
-
236
220
  def transform_value(option_defn, value)
237
221
  transformer = option_defn.transform
238
222
 
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Cri
4
4
  # The current Cri version.
5
- VERSION = '2.15.6'
5
+ VERSION = '2.15.7'
6
6
  end
@@ -126,7 +126,7 @@ module Cri
126
126
  simple_cmd.run(%w[])
127
127
  end
128
128
 
129
- assert_equal ['Awesome moo!', '', ''], lines(out)
129
+ assert_equal ['Awesome moo!', '', 'ddd=false,eee=false'], lines(out)
130
130
  assert_equal [], lines(err)
131
131
  end
132
132
 
@@ -135,7 +135,7 @@ module Cri
135
135
  simple_cmd.run(%w[abc xyz])
136
136
  end
137
137
 
138
- assert_equal ['Awesome moo!', 'abc,xyz', ''], lines(out)
138
+ assert_equal ['Awesome moo!', 'abc,xyz', 'ddd=false,eee=false'], lines(out)
139
139
  assert_equal [], lines(err)
140
140
  end
141
141
 
@@ -144,7 +144,7 @@ module Cri
144
144
  simple_cmd.run(%w[-c -b x])
145
145
  end
146
146
 
147
- assert_equal ['Awesome moo!', '', 'bbb=x,ccc=true'], lines(out)
147
+ assert_equal ['Awesome moo!', '', 'bbb=x,ccc=true,ddd=false,eee=false'], lines(out)
148
148
  assert_equal [], lines(err)
149
149
  end
150
150
 
@@ -216,7 +216,7 @@ module Cri
216
216
  simple_cmd.run(%w[-a 123])
217
217
  end
218
218
 
219
- assert_equal ['moo:123', 'Awesome moo!', '', 'aaa=123'], lines(out)
219
+ assert_equal ['moo:123', 'Awesome moo!', '', 'aaa=123,ddd=false,eee=false'], lines(out)
220
220
  assert_equal [], lines(err)
221
221
  end
222
222
 
@@ -246,7 +246,7 @@ module Cri
246
246
  nested_cmd.run(%w[sub])
247
247
  end
248
248
 
249
- assert_equal ['Sub-awesome!', '', ''], lines(out)
249
+ assert_equal ['Sub-awesome!', '', 'ddd=false,eee=false,ppp=false,qqq=false'], lines(out)
250
250
  assert_equal [], lines(err)
251
251
  end
252
252
 
@@ -297,7 +297,7 @@ module Cri
297
297
  nested_cmd.run(%w[sup])
298
298
  end
299
299
 
300
- assert_equal ['Sub-awesome!', '', ''], lines(out)
300
+ assert_equal ['Sub-awesome!', '', 'ddd=false,eee=false,ppp=false,qqq=false'], lines(out)
301
301
  assert_equal [], lines(err)
302
302
  end
303
303
 
@@ -306,7 +306,7 @@ module Cri
306
306
  nested_cmd.run(%w[-a 666 sub])
307
307
  end
308
308
 
309
- assert_equal ['super:666', 'Sub-awesome!', '', 'aaa=666'], lines(out)
309
+ assert_equal ['super:666', 'Sub-awesome!', '', 'aaa=666,ddd=false,eee=false,ppp=false,qqq=false'], lines(out)
310
310
  assert_equal [], lines(err)
311
311
  end
312
312
 
@@ -895,5 +895,73 @@ module Cri
895
895
  assert_equal ['test? true!'], lines(out)
896
896
  assert_equal [], lines(err)
897
897
  end
898
+
899
+ def test_flag_defaults_to_false
900
+ cmd = Cri::Command.define do
901
+ name 'a'
902
+ option :f, :force2, 'push with force', argument: :forbidden
903
+
904
+ run do |opts, _args, _cmd|
905
+ puts "Force? #{opts[:force2].inspect}!"
906
+ end
907
+ end
908
+
909
+ out, err = capture_io_while do
910
+ cmd.run(%w[])
911
+ end
912
+ assert_equal ['Force? false!'], lines(out)
913
+ assert_equal [], lines(err)
914
+ end
915
+
916
+ def test_required_option_defaults_to_given_value
917
+ cmd = Cri::Command.define do
918
+ name 'a'
919
+ option :a, :animal, 'specify animal', argument: :required, default: 'cow'
920
+
921
+ run do |opts, _args, _cmd|
922
+ puts "Animal = #{opts[:animal]}"
923
+ end
924
+ end
925
+
926
+ out, err = capture_io_while do
927
+ cmd.run(%w[])
928
+ end
929
+ assert_equal ['Animal = cow'], lines(out)
930
+ assert_equal [], lines(err)
931
+ end
932
+
933
+ def test_optional_option_defaults_to_given_value
934
+ cmd = Cri::Command.define do
935
+ name 'a'
936
+ option :a, :animal, 'specify animal', argument: :optional, default: 'cow'
937
+
938
+ run do |opts, _args, _cmd|
939
+ puts "Animal = #{opts[:animal]}"
940
+ end
941
+ end
942
+
943
+ out, err = capture_io_while do
944
+ cmd.run(%w[])
945
+ end
946
+ assert_equal ['Animal = cow'], lines(out)
947
+ assert_equal [], lines(err)
948
+ end
949
+
950
+ def test_required_option_defaults_to_given_value_with_transform
951
+ cmd = Cri::Command.define do
952
+ name 'a'
953
+ option :a, :animal, 'specify animal', argument: :required, transform: ->(a) { a.upcase }, default: 'cow'
954
+
955
+ run do |opts, _args, _cmd|
956
+ puts "Animal = #{opts[:animal]}"
957
+ end
958
+ end
959
+
960
+ out, err = capture_io_while do
961
+ cmd.run(%w[])
962
+ end
963
+ assert_equal ['Animal = cow'], lines(out)
964
+ assert_equal [], lines(err)
965
+ end
898
966
  end
899
967
  end
@@ -41,12 +41,12 @@ module Cri
41
41
  expected_option_definitions =
42
42
  Set.new(
43
43
  [
44
- { short: 'a', long: 'aaa', desc: 'opt a', argument: :optional, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
45
- { short: 'b', long: 'bbb', desc: 'opt b', argument: :required, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
46
- { short: 'c', long: 'ccc', desc: 'opt c', argument: :optional, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
47
- { short: 'd', long: 'ddd', desc: 'opt d', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
48
- { short: 'e', long: 'eee', desc: 'opt e', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
49
- { short: 'f', long: 'fff', desc: 'opt f', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
44
+ { short: 'a', long: 'aaa', desc: 'opt a', argument: :optional, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
45
+ { short: 'b', long: 'bbb', desc: 'opt b', argument: :required, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
46
+ { short: 'c', long: 'ccc', desc: 'opt c', argument: :optional, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
47
+ { short: 'd', long: 'ddd', desc: 'opt d', argument: :forbidden, multiple: false, hidden: false, block: nil, default: false, transform: nil },
48
+ { short: 'e', long: 'eee', desc: 'opt e', argument: :forbidden, multiple: false, hidden: false, block: nil, default: false, transform: nil },
49
+ { short: 'f', long: 'fff', desc: 'opt f', argument: :forbidden, multiple: false, hidden: true, block: nil, default: false, transform: nil },
50
50
  ],
51
51
  )
52
52
  actual_option_definitions = Set.new(command.option_definitions.map(&:to_h))
@@ -80,8 +80,8 @@ module Cri
80
80
  expected_option_definitions =
81
81
  Set.new(
82
82
  [
83
- { short: 's', long: nil, desc: 'short', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
84
- { short: nil, long: 'long', desc: 'long', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
83
+ { short: 's', long: nil, desc: 'short', argument: :forbidden, multiple: false, hidden: false, block: nil, default: false, transform: nil },
84
+ { short: nil, long: 'long', desc: 'long', argument: :forbidden, multiple: false, hidden: false, block: nil, default: false, transform: nil },
85
85
  ],
86
86
  )
87
87
  actual_option_definitions = Set.new(command.option_definitions.map(&:to_h))
@@ -104,9 +104,9 @@ module Cri
104
104
  expected_option_definitions =
105
105
  Set.new(
106
106
  [
107
- { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
108
- { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
109
- { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
107
+ { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: true, hidden: false, block: nil, default: false, transform: nil },
108
+ { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
109
+ { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
110
110
  ],
111
111
  )
112
112
  actual_option_definitions = Set.new(command.option_definitions.map(&:to_h))
@@ -129,9 +129,9 @@ module Cri
129
129
  expected_option_definitions =
130
130
  Set.new(
131
131
  [
132
- { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
133
- { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
134
- { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
132
+ { short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: false, hidden: true, block: nil, default: false, transform: nil },
133
+ { short: 'r', long: 'required', desc: 'req', argument: :required, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
134
+ { short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
135
135
  ],
136
136
  )
137
137
  actual_option_definitions = Set.new(command.option_definitions.map(&:to_h))
@@ -284,18 +284,6 @@ module Cri
284
284
  assert_equal(3, parser.options[:verbose].size)
285
285
  end
286
286
 
287
- def test_parse_with_default_required_unspecified
288
- input = %w[foo]
289
- opt_defns = [
290
- { long: 'animal', short: 'a', argument: :required, default: 'donkey' },
291
- ].map { |hash| make_opt_defn(hash) }
292
-
293
- parser = Cri::Parser.new(input, opt_defns, [], false).run
294
-
295
- assert_equal({ animal: 'donkey' }, parser.options)
296
- assert_equal(['foo'], parser.gen_argument_list.to_a)
297
- end
298
-
299
287
  def test_parse_with_default_required_no_value
300
288
  input = %w[foo -a]
301
289
  opt_defns = [
@@ -319,18 +307,6 @@ module Cri
319
307
  assert_equal(['foo'], parser.gen_argument_list.to_a)
320
308
  end
321
309
 
322
- def test_parse_with_default_optional_unspecified
323
- input = %w[foo]
324
- opt_defns = [
325
- { long: 'animal', short: 'a', argument: :optional, default: 'donkey' },
326
- ].map { |hash| make_opt_defn(hash) }
327
-
328
- parser = Cri::Parser.new(input, opt_defns, [], false).run
329
-
330
- assert_equal({ animal: 'donkey' }, parser.options)
331
- assert_equal(['foo'], parser.gen_argument_list.to_a)
332
- end
333
-
334
310
  def test_parse_with_default_optional_no_value
335
311
  input = %w[foo -a]
336
312
  opt_defns = [
@@ -451,26 +427,6 @@ module Cri
451
427
  assert_equal([], parser.gen_argument_list.to_a)
452
428
  end
453
429
 
454
- def test_parse_with_transform_default
455
- port = Class.new do
456
- def call(str)
457
- raise unless str.is_a?(String)
458
-
459
- Integer(str)
460
- end
461
- end.new
462
-
463
- input = %w[]
464
- opt_defns = [
465
- { long: 'port', short: 'p', argument: :required, default: 8080, transform: port },
466
- ].map { |hash| make_opt_defn(hash) }
467
-
468
- parser = Cri::Parser.new(input, opt_defns, [], false).run
469
-
470
- assert_equal({ port: 8080 }, parser.options)
471
- assert_equal([], parser.gen_argument_list.to_a)
472
- end
473
-
474
430
  def test_parse_with_transform_exception
475
431
  input = %w[--port one_hundred_and_twenty_three]
476
432
  opt_defns = [
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cri
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.15.6
4
+ version: 2.15.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Defreyne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-19 00:00:00.000000000 Z
11
+ date: 2019-05-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Cri allows building easy-to-use command-line interfaces with support
14
14
  for subcommands.