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 +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +8 -3
- data/NEWS.md +6 -0
- data/README.md +7 -0
- data/lib/cri/command.rb +14 -0
- data/lib/cri/option_definition.rb +2 -0
- data/lib/cri/parser.rb +0 -16
- data/lib/cri/version.rb +1 -1
- data/test/test_command.rb +75 -7
- data/test/test_command_dsl.rb +14 -14
- data/test/test_parser.rb +0 -44
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 961c45fae4395d1aad6d4f3d79ccf4b00c32bbd38f816d52df500aea7c782da5
|
4
|
+
data.tar.gz: 7917db80f32845b87f5cc6942623a1ee34e5d83058cc526100afc07ea412b246
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48b820bcb56c95e6ff4594db88a814c72b5c10868e971a03236d47d4826af789228e9f38e6b7d926450bbb0700aac84d575b4cd613ad5a20f8adfcfa5f220606
|
7
|
+
data.tar.gz: f84359f5f263d1ab38400a8b4667e8b47c2cf0b9a8ef883e8896e7c86a39b5ecc874e146f215a855c116d1863e68e62f29164c50a61f99e00ec07938ca85aaee
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
cri (2.15.
|
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.
|
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.
|
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
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
|
data/lib/cri/command.rb
CHANGED
@@ -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
|
data/lib/cri/parser.rb
CHANGED
@@ -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
|
|
data/lib/cri/version.rb
CHANGED
data/test/test_command.rb
CHANGED
@@ -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
|
data/test/test_command_dsl.rb
CHANGED
@@ -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,
|
45
|
-
{ short: 'b', long: 'bbb', desc: 'opt b', argument: :required, multiple: false, hidden: false, block: nil, default: nil,
|
46
|
-
{ short: 'c', long: 'ccc', desc: 'opt c', argument: :optional, multiple: false, hidden: false, block: nil, default: nil,
|
47
|
-
{ short: 'd', long: 'ddd', desc: 'opt d', argument: :forbidden, multiple: false, hidden: false, block: nil, default:
|
48
|
-
{ short: 'e', long: 'eee', desc: 'opt e', argument: :forbidden, multiple: false, hidden: false, block: nil, default:
|
49
|
-
{ short: 'f', long: 'fff', desc: 'opt f', argument: :forbidden, multiple: false, hidden: true, block: nil, default:
|
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:
|
84
|
-
{ short: nil, long: 'long', desc: 'long', argument: :forbidden, multiple: false, hidden: false, block: nil, default:
|
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:
|
108
|
-
{ short: 'r', long: 'required', desc: 'req', argument: :required, multiple: true, hidden: false, block: nil, default: nil,
|
109
|
-
{ short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: true, hidden: false, block: nil, default: 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:
|
133
|
-
{ short: 'r', long: 'required', desc: 'req', argument: :required, multiple: false, hidden: true, block: nil, default: nil,
|
134
|
-
{ short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: false, hidden: true, block: nil, default: 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))
|
data/test/test_parser.rb
CHANGED
@@ -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.
|
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-
|
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.
|