cri 2.6.1 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +46 -21
- data/LICENSE +1 -1
- data/NEWS.md +12 -0
- data/README.adoc +1 -1
- data/Rakefile +7 -2
- data/cri.gemspec +5 -7
- data/lib/cri.rb +0 -2
- data/lib/cri/argument_array.rb +0 -4
- data/lib/cri/command.rb +31 -40
- data/lib/cri/command_dsl.rb +28 -18
- data/lib/cri/command_runner.rb +2 -6
- data/lib/cri/commands/basic_help.rb +2 -2
- data/lib/cri/commands/basic_root.rb +1 -1
- data/lib/cri/core_ext.rb +3 -1
- data/lib/cri/core_ext/string.rb +28 -30
- data/lib/cri/help_renderer.rb +105 -23
- data/lib/cri/option_parser.rb +13 -17
- data/lib/cri/platform.rb +1 -5
- data/lib/cri/string_formatter.rb +14 -9
- data/lib/cri/version.rb +1 -3
- data/test/helper.rb +38 -38
- data/test/test_argument_array.rb +7 -7
- data/test/test_base.rb +4 -4
- data/test/test_basic_help.rb +47 -47
- data/test/test_basic_root.rb +10 -10
- data/test/test_command.rb +424 -347
- data/test/test_command_dsl.rb +174 -150
- data/test/test_command_runner.rb +22 -23
- data/test/test_option_parser.rb +212 -224
- data/test/test_string_formatter.rb +97 -82
- metadata +3 -3
data/test/test_command.rb
CHANGED
@@ -1,488 +1,565 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
3
|
+
module Cri
|
4
|
+
class CommandTestCase < Cri::TestCase
|
5
|
+
def simple_cmd
|
6
|
+
Cri::Command.define do
|
7
|
+
name 'moo'
|
8
|
+
usage 'moo [options] arg1 arg2 ...'
|
9
|
+
summary 'does stuff'
|
10
|
+
description 'This command does a lot of stuff.'
|
11
|
+
|
12
|
+
option :a, :aaa, 'opt a', :argument => :optional do |value, cmd|
|
13
|
+
$stdout.puts "#{cmd.name}:#{value}"
|
14
|
+
end
|
15
|
+
required :b, :bbb, 'opt b'
|
16
|
+
optional :c, :ccc, 'opt c'
|
17
|
+
flag :d, :ddd, 'opt d'
|
18
|
+
forbidden :e, :eee, 'opt e'
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
run do |opts, args, c|
|
21
|
+
$stdout.puts "Awesome #{c.name}!"
|
22
22
|
|
23
|
-
|
23
|
+
$stdout.puts args.join(',')
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
opts_strings = []
|
26
|
+
opts.each_pair { |k, v| opts_strings << "#{k}=#{v}" }
|
27
|
+
$stdout.puts opts_strings.sort.join(',')
|
28
|
+
end
|
28
29
|
end
|
29
30
|
end
|
30
|
-
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
def bare_cmd
|
33
|
+
Cri::Command.define do
|
34
|
+
name 'moo'
|
35
35
|
|
36
|
-
|
36
|
+
run do |_opts, _args|
|
37
|
+
end
|
37
38
|
end
|
38
39
|
end
|
39
|
-
end
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
def nested_cmd
|
42
|
+
super_cmd = Cri::Command.define do
|
43
|
+
name 'super'
|
44
|
+
usage 'super [command] [options] [arguments]'
|
45
|
+
summary 'does super stuff'
|
46
|
+
description 'This command does super stuff.'
|
47
47
|
|
48
|
-
|
49
|
-
|
48
|
+
option :a, :aaa, 'opt a', :argument => :optional do |value, cmd|
|
49
|
+
$stdout.puts "#{cmd.name}:#{value}"
|
50
|
+
end
|
51
|
+
required :b, :bbb, 'opt b'
|
52
|
+
optional :c, :ccc, 'opt c'
|
53
|
+
flag :d, :ddd, 'opt d'
|
54
|
+
forbidden :e, :eee, 'opt e'
|
50
55
|
end
|
51
|
-
required :b, :bbb, 'opt b'
|
52
|
-
optional :c, :ccc, 'opt c'
|
53
|
-
flag :d, :ddd, 'opt d'
|
54
|
-
forbidden :e, :eee, 'opt e'
|
55
|
-
end
|
56
56
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
57
|
+
super_cmd.define_command do
|
58
|
+
name 'sub'
|
59
|
+
aliases 'sup'
|
60
|
+
usage 'sub [options]'
|
61
|
+
summary 'does subby stuff'
|
62
|
+
description 'This command does subby stuff.'
|
63
63
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
64
|
+
option :m, :mmm, 'opt m', :argument => :optional
|
65
|
+
required :n, :nnn, 'opt n'
|
66
|
+
optional :o, :ooo, 'opt o'
|
67
|
+
flag :p, :ppp, 'opt p'
|
68
|
+
forbidden :q, :qqq, 'opt q'
|
69
69
|
|
70
|
-
|
71
|
-
|
70
|
+
run do |opts, args|
|
71
|
+
$stdout.puts 'Sub-awesome!'
|
72
72
|
|
73
|
-
|
73
|
+
$stdout.puts args.join(',')
|
74
74
|
|
75
|
-
|
76
|
-
|
77
|
-
|
75
|
+
opts_strings = []
|
76
|
+
opts.each_pair { |k, v| opts_strings << "#{k}=#{v}" }
|
77
|
+
$stdout.puts opts_strings.join(',')
|
78
|
+
end
|
78
79
|
end
|
79
|
-
end
|
80
80
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
81
|
+
super_cmd.define_command do
|
82
|
+
name 'sink'
|
83
|
+
usage 'sink thing_to_sink'
|
84
|
+
summary 'sinks stuff'
|
85
|
+
description 'Sinks stuff (like ships and the like).'
|
86
86
|
|
87
|
-
|
87
|
+
run do |_opts, _args|
|
88
|
+
end
|
88
89
|
end
|
89
|
-
end
|
90
90
|
|
91
|
-
|
92
|
-
|
91
|
+
super_cmd
|
92
|
+
end
|
93
93
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
94
|
+
def nested_cmd_with_run_block
|
95
|
+
super_cmd = Cri::Command.define do
|
96
|
+
name 'super'
|
97
|
+
usage 'super [command] [options] [arguments]'
|
98
|
+
summary 'does super stuff'
|
99
|
+
description 'This command does super stuff.'
|
100
100
|
|
101
|
-
|
102
|
-
|
101
|
+
run do |_opts, _args|
|
102
|
+
$stdout.puts 'super'
|
103
|
+
end
|
103
104
|
end
|
104
|
-
end
|
105
105
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
106
|
+
super_cmd.define_command do
|
107
|
+
name 'sub'
|
108
|
+
aliases 'sup'
|
109
|
+
usage 'sub [options]'
|
110
|
+
summary 'does subby stuff'
|
111
|
+
description 'This command does subby stuff.'
|
112
112
|
|
113
|
-
|
114
|
-
|
113
|
+
run do |_opts, _args|
|
114
|
+
$stdout.puts 'sub'
|
115
|
+
end
|
115
116
|
end
|
117
|
+
|
118
|
+
super_cmd
|
116
119
|
end
|
117
120
|
|
118
|
-
|
119
|
-
|
121
|
+
def test_invoke_simple_without_opts_or_args
|
122
|
+
out, err = capture_io_while do
|
123
|
+
simple_cmd.run(%w())
|
124
|
+
end
|
120
125
|
|
121
|
-
|
122
|
-
|
123
|
-
simple_cmd.run(%w())
|
126
|
+
assert_equal ['Awesome moo!', '', ''], lines(out)
|
127
|
+
assert_equal [], lines(err)
|
124
128
|
end
|
125
129
|
|
126
|
-
|
127
|
-
|
128
|
-
|
130
|
+
def test_invoke_simple_with_args
|
131
|
+
out, err = capture_io_while do
|
132
|
+
simple_cmd.run(%w(abc xyz))
|
133
|
+
end
|
129
134
|
|
130
|
-
|
131
|
-
|
132
|
-
simple_cmd.run(%w(abc xyz))
|
135
|
+
assert_equal ['Awesome moo!', 'abc,xyz', ''], lines(out)
|
136
|
+
assert_equal [], lines(err)
|
133
137
|
end
|
134
138
|
|
135
|
-
|
136
|
-
|
137
|
-
|
139
|
+
def test_invoke_simple_with_opts
|
140
|
+
out, err = capture_io_while do
|
141
|
+
simple_cmd.run(%w(-c -b x))
|
142
|
+
end
|
138
143
|
|
139
|
-
|
140
|
-
|
141
|
-
simple_cmd.run(%w(-c -b x))
|
144
|
+
assert_equal ['Awesome moo!', '', 'bbb=x,ccc=true'], lines(out)
|
145
|
+
assert_equal [], lines(err)
|
142
146
|
end
|
143
147
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
out, err = capture_io_while do
|
150
|
-
assert_raises SystemExit do
|
151
|
-
simple_cmd.run(%w( -b ))
|
148
|
+
def test_invoke_simple_with_missing_opt_arg
|
149
|
+
out, err = capture_io_while do
|
150
|
+
assert_raises SystemExit do
|
151
|
+
simple_cmd.run(%w( -b ))
|
152
|
+
end
|
152
153
|
end
|
153
|
-
end
|
154
154
|
|
155
|
-
|
156
|
-
|
157
|
-
|
155
|
+
assert_equal [], lines(out)
|
156
|
+
assert_equal ['moo: option requires an argument -- b'], lines(err)
|
157
|
+
end
|
158
158
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
159
|
+
def test_invoke_simple_with_illegal_opt
|
160
|
+
out, err = capture_io_while do
|
161
|
+
assert_raises SystemExit do
|
162
|
+
simple_cmd.run(%w( -z ))
|
163
|
+
end
|
163
164
|
end
|
165
|
+
|
166
|
+
assert_equal [], lines(out)
|
167
|
+
assert_equal ['moo: illegal option -- z'], lines(err)
|
164
168
|
end
|
165
169
|
|
166
|
-
|
167
|
-
|
168
|
-
|
170
|
+
def test_invoke_simple_with_opt_with_block
|
171
|
+
out, err = capture_io_while do
|
172
|
+
simple_cmd.run(%w( -a 123 ))
|
173
|
+
end
|
169
174
|
|
170
|
-
|
171
|
-
|
172
|
-
simple_cmd.run(%w( -a 123 ))
|
175
|
+
assert_equal ['moo:123', 'Awesome moo!', '', 'aaa=123'], lines(out)
|
176
|
+
assert_equal [], lines(err)
|
173
177
|
end
|
174
178
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
out, err = capture_io_while do
|
181
|
-
assert_raises SystemExit do
|
182
|
-
nested_cmd.run(%w())
|
179
|
+
def test_invoke_nested_without_opts_or_args
|
180
|
+
out, err = capture_io_while do
|
181
|
+
assert_raises SystemExit do
|
182
|
+
nested_cmd.run(%w())
|
183
|
+
end
|
183
184
|
end
|
185
|
+
|
186
|
+
assert_equal [], lines(out)
|
187
|
+
assert_equal ['super: no command given'], lines(err)
|
184
188
|
end
|
185
189
|
|
186
|
-
|
187
|
-
|
188
|
-
|
190
|
+
def test_invoke_nested_with_correct_command_name
|
191
|
+
out, err = capture_io_while do
|
192
|
+
nested_cmd.run(%w( sub ))
|
193
|
+
end
|
189
194
|
|
190
|
-
|
191
|
-
|
192
|
-
nested_cmd.run(%w( sub ))
|
195
|
+
assert_equal ['Sub-awesome!', '', ''], lines(out)
|
196
|
+
assert_equal [], lines(err)
|
193
197
|
end
|
194
198
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
out, err = capture_io_while do
|
201
|
-
assert_raises SystemExit do
|
202
|
-
nested_cmd.run(%w( oogabooga ))
|
199
|
+
def test_invoke_nested_with_incorrect_command_name
|
200
|
+
out, err = capture_io_while do
|
201
|
+
assert_raises SystemExit do
|
202
|
+
nested_cmd.run(%w( oogabooga ))
|
203
|
+
end
|
203
204
|
end
|
204
|
-
end
|
205
205
|
|
206
|
-
|
207
|
-
|
208
|
-
|
206
|
+
assert_equal [], lines(out)
|
207
|
+
assert_equal ["super: unknown command 'oogabooga'"], lines(err)
|
208
|
+
end
|
209
209
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
210
|
+
def test_invoke_nested_with_ambiguous_command_name
|
211
|
+
out, err = capture_io_while do
|
212
|
+
assert_raises SystemExit do
|
213
|
+
nested_cmd.run(%w( s ))
|
214
|
+
end
|
214
215
|
end
|
216
|
+
|
217
|
+
assert_equal [], lines(out)
|
218
|
+
assert_equal ["super: 's' is ambiguous:", ' sink sub'], lines(err)
|
215
219
|
end
|
216
220
|
|
217
|
-
|
218
|
-
|
219
|
-
|
221
|
+
def test_invoke_nested_with_alias
|
222
|
+
out, err = capture_io_while do
|
223
|
+
nested_cmd.run(%w( sup ))
|
224
|
+
end
|
220
225
|
|
221
|
-
|
222
|
-
|
223
|
-
nested_cmd.run(%w( sup ))
|
226
|
+
assert_equal ['Sub-awesome!', '', ''], lines(out)
|
227
|
+
assert_equal [], lines(err)
|
224
228
|
end
|
225
229
|
|
226
|
-
|
227
|
-
|
228
|
-
|
230
|
+
def test_invoke_nested_with_options_before_command
|
231
|
+
out, err = capture_io_while do
|
232
|
+
nested_cmd.run(%w( -a 666 sub ))
|
233
|
+
end
|
229
234
|
|
230
|
-
|
231
|
-
|
232
|
-
nested_cmd.run(%w( -a 666 sub ))
|
235
|
+
assert_equal ['super:666', 'Sub-awesome!', '', 'aaa=666'], lines(out)
|
236
|
+
assert_equal [], lines(err)
|
233
237
|
end
|
234
238
|
|
235
|
-
|
236
|
-
|
237
|
-
|
239
|
+
def test_invoke_nested_with_run_block
|
240
|
+
out, err = capture_io_while do
|
241
|
+
nested_cmd_with_run_block.run(%w())
|
242
|
+
end
|
238
243
|
|
239
|
-
|
240
|
-
|
241
|
-
nested_cmd_with_run_block.run(%w())
|
242
|
-
end
|
244
|
+
assert_equal ['super'], lines(out)
|
245
|
+
assert_equal [], lines(err)
|
243
246
|
|
244
|
-
|
245
|
-
|
247
|
+
out, err = capture_io_while do
|
248
|
+
nested_cmd_with_run_block.run(%w( sub ))
|
249
|
+
end
|
246
250
|
|
247
|
-
|
248
|
-
|
251
|
+
assert_equal ['sub'], lines(out)
|
252
|
+
assert_equal [], lines(err)
|
249
253
|
end
|
250
254
|
|
251
|
-
|
252
|
-
|
253
|
-
|
255
|
+
def test_help_nested
|
256
|
+
def $stdout.tty?
|
257
|
+
true
|
258
|
+
end
|
254
259
|
|
255
|
-
|
256
|
-
def $stdout.tty? ; true ; end
|
260
|
+
help = nested_cmd.subcommands.find { |cmd| cmd.name == 'sub' }.help
|
257
261
|
|
258
|
-
|
262
|
+
assert help.include?("USAGE\e[0m\e[0m\n \e[32msuper\e[0m \e[32msub\e[0m [options]\n")
|
263
|
+
end
|
259
264
|
|
260
|
-
|
261
|
-
|
265
|
+
def test_help_with_and_without_colors
|
266
|
+
def $stdout.tty?
|
267
|
+
true
|
268
|
+
end
|
269
|
+
help_on_tty = simple_cmd.help
|
270
|
+
def $stdout.tty?
|
271
|
+
false
|
272
|
+
end
|
273
|
+
help_not_on_tty = simple_cmd.help
|
262
274
|
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
def $stdout.tty? ; false ; end
|
267
|
-
help_not_on_tty = simple_cmd.help
|
275
|
+
assert_includes help_on_tty, "\e[31mUSAGE\e[0m\e[0m\n \e[32mmoo"
|
276
|
+
assert_includes help_not_on_tty, "USAGE\n moo"
|
277
|
+
end
|
268
278
|
|
269
|
-
|
270
|
-
|
271
|
-
|
279
|
+
def test_help_for_bare_cmd
|
280
|
+
bare_cmd.help
|
281
|
+
end
|
272
282
|
|
273
|
-
|
274
|
-
|
275
|
-
|
283
|
+
def test_help_with_optional_options
|
284
|
+
def $stdout.tty?
|
285
|
+
true
|
286
|
+
end
|
276
287
|
|
277
|
-
|
278
|
-
|
288
|
+
cmd = Cri::Command.define do
|
289
|
+
name 'build'
|
290
|
+
flag :s, nil, 'short'
|
291
|
+
flag nil, :long, 'long'
|
292
|
+
end
|
293
|
+
help = cmd.help
|
279
294
|
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
flag nil, :long, 'long'
|
295
|
+
assert_match(/--long.*-s/m, help)
|
296
|
+
assert_match(/^ \e\[33m--long\e\[0m long$/, help)
|
297
|
+
assert_match(/^ \e\[33m-s\e\[0m short$/, help)
|
284
298
|
end
|
285
|
-
help = cmd.help
|
286
|
-
|
287
|
-
assert_match(/--long.*-s/m, help)
|
288
|
-
assert_match(/^\e\[33m --long \e\[0mlong$/, help)
|
289
|
-
assert_match(/^\e\[33m -s \e\[0mshort$/, help)
|
290
|
-
end
|
291
299
|
|
292
|
-
|
293
|
-
|
300
|
+
def test_help_with_different_option_types_short_and_long
|
301
|
+
def $stdout.tty?
|
302
|
+
true
|
303
|
+
end
|
294
304
|
|
295
|
-
|
296
|
-
|
305
|
+
cmd = Cri::Command.define do
|
306
|
+
name 'build'
|
307
|
+
required :r, :required, 'required value'
|
308
|
+
flag :f, :flag, 'forbidden value'
|
309
|
+
optional :o, :optional, 'optional value'
|
310
|
+
end
|
311
|
+
help = cmd.help
|
297
312
|
|
298
|
-
|
299
|
-
|
300
|
-
|
313
|
+
assert_match(/^ \e\[33m-r\e\[0m \e\[33m--required\e\[0m=<value> required value$/, help)
|
314
|
+
assert_match(/^ \e\[33m-f\e\[0m \e\[33m--flag\e\[0m forbidden value$/, help)
|
315
|
+
assert_match(/^ \e\[33m-o\e\[0m \e\[33m--optional\e\[0m\[=<value>\] optional value$/, help)
|
301
316
|
end
|
302
|
-
assert_equal 'build', cmd.name
|
303
317
|
|
304
|
-
|
305
|
-
|
306
|
-
|
318
|
+
def test_help_with_different_option_types_short
|
319
|
+
def $stdout.tty?
|
320
|
+
true
|
321
|
+
end
|
307
322
|
|
308
|
-
|
309
|
-
|
323
|
+
cmd = Cri::Command.define do
|
324
|
+
name 'build'
|
325
|
+
required :r, nil, 'required value'
|
326
|
+
flag :f, nil, 'forbidden value'
|
327
|
+
optional :o, nil, 'optional value'
|
328
|
+
end
|
329
|
+
help = cmd.help
|
310
330
|
|
311
|
-
|
312
|
-
|
313
|
-
|
331
|
+
assert_match(/^ \e\[33m-r\e\[0m <value> required value$/, help)
|
332
|
+
assert_match(/^ \e\[33m-f\e\[0m forbidden value$/, help)
|
333
|
+
assert_match(/^ \e\[33m-o\e\[0m \[<value>\] optional value$/, help)
|
314
334
|
end
|
315
|
-
assert_equal 'build', cmd.name
|
316
335
|
|
317
|
-
|
318
|
-
|
336
|
+
def test_help_with_different_option_types_long
|
337
|
+
def $stdout.tty?
|
338
|
+
true
|
339
|
+
end
|
340
|
+
|
341
|
+
cmd = Cri::Command.define do
|
342
|
+
name 'build'
|
343
|
+
required nil, :required, 'required value'
|
344
|
+
flag nil, :flag, 'forbidden value'
|
345
|
+
optional nil, :optional, 'optional value'
|
346
|
+
end
|
347
|
+
help = cmd.help
|
348
|
+
|
349
|
+
assert_match(/^ \e\[33m--required\e\[0m=<value> required value$/, help)
|
350
|
+
assert_match(/^ \e\[33m--flag\e\[0m forbidden value$/, help)
|
351
|
+
assert_match(/^ \e\[33m--optional\e\[0m\[=<value>\] optional value$/, help)
|
319
352
|
end
|
320
353
|
|
321
|
-
|
322
|
-
|
354
|
+
def test_help_with_multiple_groups
|
355
|
+
help = nested_cmd.subcommands.find { |cmd| cmd.name == 'sub' }.help
|
323
356
|
|
324
|
-
|
325
|
-
cmd = Cri::Command.new_basic_root.modify do
|
326
|
-
name 'mytool'
|
357
|
+
assert_match(/OPTIONS.*OPTIONS FOR SUPER/m, help)
|
327
358
|
end
|
328
359
|
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
360
|
+
def test_modify_with_block_argument
|
361
|
+
cmd = Cri::Command.define do |c|
|
362
|
+
c.name 'build'
|
363
|
+
end
|
364
|
+
assert_equal 'build', cmd.name
|
333
365
|
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
end
|
366
|
+
cmd.modify do |c|
|
367
|
+
c.name 'compile'
|
368
|
+
end
|
338
369
|
|
339
|
-
|
340
|
-
cmd = Cri::Command.define do |c|
|
341
|
-
c.name 'moo'
|
370
|
+
assert_equal 'compile', cmd.name
|
342
371
|
end
|
343
372
|
|
344
|
-
|
345
|
-
|
373
|
+
def test_help_with_wrapped_options
|
374
|
+
def $stdout.tty?
|
375
|
+
true
|
376
|
+
end
|
377
|
+
|
378
|
+
cmd = Cri::Command.define do
|
379
|
+
name 'build'
|
380
|
+
flag nil, :longflag, 'This is an option with a very long description that should be wrapped'
|
381
|
+
end
|
382
|
+
help = cmd.help
|
346
383
|
|
347
|
-
|
348
|
-
|
349
|
-
name 'moo'
|
384
|
+
assert_match(/^ \e\[33m--longflag\e\[0m This is an option with a very long description that$/, help)
|
385
|
+
assert_match(/^ should be wrapped$/, help)
|
350
386
|
end
|
351
387
|
|
352
|
-
|
353
|
-
|
388
|
+
def test_modify_without_block_argument
|
389
|
+
cmd = Cri::Command.define do
|
390
|
+
name 'build'
|
391
|
+
end
|
392
|
+
assert_equal 'build', cmd.name
|
393
|
+
|
394
|
+
cmd.modify do
|
395
|
+
name 'compile'
|
396
|
+
end
|
354
397
|
|
355
|
-
|
356
|
-
cmd = bare_cmd
|
357
|
-
cmd.define_command do |c|
|
358
|
-
c.name 'baresub'
|
398
|
+
assert_equal 'compile', cmd.name
|
359
399
|
end
|
360
400
|
|
361
|
-
|
362
|
-
|
401
|
+
def test_new_basic_root
|
402
|
+
cmd = Cri::Command.new_basic_root.modify do
|
403
|
+
name 'mytool'
|
404
|
+
end
|
405
|
+
|
406
|
+
# Check option definitions
|
407
|
+
assert_equal 1, cmd.option_definitions.size
|
408
|
+
opt_def = cmd.option_definitions.to_a[0]
|
409
|
+
assert_equal 'help', opt_def[:long]
|
363
410
|
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
name 'baresub'
|
411
|
+
# Check subcommand
|
412
|
+
assert_equal 1, cmd.subcommands.size
|
413
|
+
assert_equal 'help', cmd.subcommands.to_a[0].name
|
368
414
|
end
|
369
415
|
|
370
|
-
|
371
|
-
|
416
|
+
def test_define_with_block_argument
|
417
|
+
cmd = Cri::Command.define do |c|
|
418
|
+
c.name 'moo'
|
419
|
+
end
|
372
420
|
|
373
|
-
|
374
|
-
error = assert_raises RuntimeError do
|
375
|
-
Cri::Command.define('raise "boom"', 'mycommand.rb')
|
421
|
+
assert_equal 'moo', cmd.name
|
376
422
|
end
|
377
423
|
|
378
|
-
|
379
|
-
|
424
|
+
def test_define_without_block_argument
|
425
|
+
cmd = Cri::Command.define do
|
426
|
+
name 'moo'
|
427
|
+
end
|
380
428
|
|
381
|
-
|
382
|
-
cmd = nested_cmd
|
383
|
-
subcmd = simple_cmd
|
384
|
-
cmd.add_command subcmd
|
385
|
-
subcmd.modify do |c|
|
386
|
-
c.name 'old-and-deprecated'
|
387
|
-
c.summary 'does stuff the ancient, totally deprecated way'
|
388
|
-
c.be_hidden
|
429
|
+
assert_equal 'moo', cmd.name
|
389
430
|
end
|
390
431
|
|
391
|
-
|
392
|
-
|
393
|
-
|
432
|
+
def test_define_subcommand_with_block_argument
|
433
|
+
cmd = bare_cmd
|
434
|
+
cmd.define_command do |c|
|
435
|
+
c.name 'baresub'
|
436
|
+
end
|
394
437
|
|
395
|
-
|
396
|
-
|
397
|
-
assert cmd.help(:verbose => true).include?('old-and-deprecated')
|
398
|
-
end
|
438
|
+
assert_equal 'baresub', cmd.subcommands.to_a[0].name
|
439
|
+
end
|
399
440
|
|
400
|
-
|
401
|
-
|
441
|
+
def test_define_subcommand_without_block_argument
|
442
|
+
cmd = bare_cmd
|
443
|
+
cmd.define_command do
|
444
|
+
name 'baresub'
|
445
|
+
end
|
402
446
|
|
403
|
-
|
404
|
-
cmd.add_command subcmd
|
405
|
-
subcmd.modify do |c|
|
406
|
-
c.name 'first'
|
407
|
-
c.summary 'does stuff first'
|
447
|
+
assert_equal 'baresub', cmd.subcommands.to_a[0].name
|
408
448
|
end
|
409
449
|
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
450
|
+
def test_backtrace_includes_filename
|
451
|
+
error = assert_raises RuntimeError do
|
452
|
+
Cri::Command.define('raise "boom"', 'mycommand.rb')
|
453
|
+
end
|
454
|
+
|
455
|
+
assert_match(/mycommand.rb/, error.backtrace.join("\n"))
|
416
456
|
end
|
417
457
|
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
458
|
+
def test_hidden_commands_single
|
459
|
+
cmd = nested_cmd
|
460
|
+
subcmd = simple_cmd
|
461
|
+
cmd.add_command subcmd
|
462
|
+
subcmd.modify do |c|
|
463
|
+
c.name 'old-and-deprecated'
|
464
|
+
c.summary 'does stuff the ancient, totally deprecated way'
|
465
|
+
c.be_hidden
|
466
|
+
end
|
467
|
+
|
468
|
+
refute cmd.help.include?('hidden commands omitted')
|
469
|
+
assert cmd.help.include?('hidden command omitted')
|
470
|
+
refute cmd.help.include?('old-and-deprecated')
|
471
|
+
|
472
|
+
refute cmd.help(:verbose => true).include?('hidden commands omitted')
|
473
|
+
refute cmd.help(:verbose => true).include?('hidden command omitted')
|
474
|
+
assert cmd.help(:verbose => true).include?('old-and-deprecated')
|
424
475
|
end
|
425
476
|
|
426
|
-
|
427
|
-
|
428
|
-
refute cmd.help.include?('old-and-deprecated')
|
429
|
-
refute cmd.help.include?('ancient-and-deprecated')
|
477
|
+
def test_hidden_commands_multiple
|
478
|
+
cmd = nested_cmd
|
430
479
|
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
480
|
+
subcmd = simple_cmd
|
481
|
+
cmd.add_command subcmd
|
482
|
+
subcmd.modify do |c|
|
483
|
+
c.name 'first'
|
484
|
+
c.summary 'does stuff first'
|
485
|
+
end
|
435
486
|
|
436
|
-
|
437
|
-
|
438
|
-
|
487
|
+
subcmd = simple_cmd
|
488
|
+
cmd.add_command subcmd
|
489
|
+
subcmd.modify do |c|
|
490
|
+
c.name 'old-and-deprecated'
|
491
|
+
c.summary 'does stuff the old, deprecated way'
|
492
|
+
c.be_hidden
|
493
|
+
end
|
439
494
|
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
495
|
+
subcmd = simple_cmd
|
496
|
+
cmd.add_command subcmd
|
497
|
+
subcmd.modify do |c|
|
498
|
+
c.name 'ancient-and-deprecated'
|
499
|
+
c.summary 'does stuff the ancient, reallydeprecated way'
|
500
|
+
c.be_hidden
|
445
501
|
end
|
446
|
-
end
|
447
502
|
|
448
|
-
|
449
|
-
cmd.
|
450
|
-
|
451
|
-
|
452
|
-
end
|
503
|
+
assert cmd.help.include?('hidden commands omitted')
|
504
|
+
refute cmd.help.include?('hidden command omitted')
|
505
|
+
refute cmd.help.include?('old-and-deprecated')
|
506
|
+
refute cmd.help.include?('ancient-and-deprecated')
|
453
507
|
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
508
|
+
refute cmd.help(:verbose => true).include?('hidden commands omitted')
|
509
|
+
refute cmd.help(:verbose => true).include?('hidden command omitted')
|
510
|
+
assert cmd.help(:verbose => true).include?('old-and-deprecated')
|
511
|
+
assert cmd.help(:verbose => true).include?('ancient-and-deprecated')
|
458
512
|
|
459
|
-
|
460
|
-
cmd.
|
513
|
+
pattern = /ancient-and-deprecated.*first.*old-and-deprecated/m
|
514
|
+
assert_match(pattern, cmd.help(:verbose => true))
|
461
515
|
end
|
462
|
-
end
|
463
516
|
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
puts "args=#{arguments.join(',')} args.raw=#{arguments.raw.join(',')}"
|
517
|
+
def test_run_with_raw_args
|
518
|
+
cmd = Cri::Command.define do
|
519
|
+
name 'moo'
|
520
|
+
run do |_opts, args|
|
521
|
+
puts "args=#{args.join(',')} args.raw=#{args.raw.join(',')}"
|
470
522
|
end
|
471
|
-
end
|
523
|
+
end
|
524
|
+
|
525
|
+
out, _err = capture_io_while do
|
526
|
+
cmd.run(%w( foo -- bar ))
|
527
|
+
end
|
528
|
+
assert_equal "args=foo,bar args.raw=foo,--,bar\n", out
|
529
|
+
end
|
530
|
+
|
531
|
+
def test_run_without_block
|
532
|
+
cmd = Cri::Command.define do
|
533
|
+
name 'moo'
|
534
|
+
end
|
535
|
+
|
536
|
+
assert_raises(Cri::NotImplementedError) do
|
537
|
+
cmd.run([])
|
538
|
+
end
|
472
539
|
end
|
473
540
|
|
474
|
-
|
475
|
-
cmd
|
541
|
+
def test_runner_with_raw_args
|
542
|
+
cmd = Cri::Command.define do
|
543
|
+
name 'moo'
|
544
|
+
runner(Class.new(Cri::CommandRunner) do
|
545
|
+
def run
|
546
|
+
puts "args=#{arguments.join(',')} args.raw=#{arguments.raw.join(',')}"
|
547
|
+
end
|
548
|
+
end)
|
549
|
+
end
|
550
|
+
|
551
|
+
out, _err = capture_io_while do
|
552
|
+
cmd.run(%w( foo -- bar ))
|
553
|
+
end
|
554
|
+
assert_equal "args=foo,bar args.raw=foo,--,bar\n", out
|
476
555
|
end
|
477
|
-
assert_equal "args=foo,bar args.raw=foo,--,bar\n", out
|
478
|
-
end
|
479
556
|
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
557
|
+
def test_compare
|
558
|
+
foo = Cri::Command.define { name 'foo' }
|
559
|
+
bar = Cri::Command.define { name 'bar' }
|
560
|
+
qux = Cri::Command.define { name 'qux' }
|
484
561
|
|
485
|
-
|
562
|
+
assert_equal [bar, foo, qux], [foo, bar, qux].sort
|
563
|
+
end
|
486
564
|
end
|
487
|
-
|
488
565
|
end
|