benry-cmdopt 2.3.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +6 -0
- data/README.md +50 -6
- data/benry-cmdopt.gemspec +1 -1
- data/doc/benry-cmdopt.html +45 -6
- data/lib/benry/cmdopt.rb +31 -11
- data/test/cmdopt_test.rb +1 -1574
- data/test/facade_test.rb +197 -0
- data/test/item_test.rb +463 -0
- data/test/parser_test.rb +294 -0
- data/test/run_all.rb +6 -0
- data/test/schema_test.rb +727 -0
- data/test/shared.rb +6 -0
- metadata +12 -2
data/test/schema_test.rb
ADDED
@@ -0,0 +1,727 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative './shared'
|
5
|
+
|
6
|
+
|
7
|
+
Oktest.scope do
|
8
|
+
|
9
|
+
|
10
|
+
def new_test_schema()
|
11
|
+
sc = Benry::CmdOpt::Schema.new
|
12
|
+
sc.add(:help , "-h, --help" , "show help message.")
|
13
|
+
sc.add(:version, "--version" , "print version")
|
14
|
+
sc.add(:file , "-f, --file=<FILE>" , "filename")
|
15
|
+
sc.add(:indent , "-i, --indent[=<WIDTH>]", "enable indent", type: Integer)
|
16
|
+
sc
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
topic Benry::CmdOpt::Schema do
|
21
|
+
|
22
|
+
|
23
|
+
topic '#parse()' do
|
24
|
+
|
25
|
+
before do
|
26
|
+
@schema = Benry::CmdOpt::Schema.new
|
27
|
+
end
|
28
|
+
|
29
|
+
spec "[!qw0ac] parses command option definition string." do
|
30
|
+
sc = @schema
|
31
|
+
tuple = sc.__send__(:parse_optdef, "-h, --help")
|
32
|
+
ok {tuple} == ['h', 'help', nil, nil]
|
33
|
+
tuple = sc.__send__(:parse_optdef, "-h")
|
34
|
+
ok {tuple} == ['h', nil, nil, nil]
|
35
|
+
tuple = sc.__send__(:parse_optdef, "--help")
|
36
|
+
ok {tuple} == [nil, 'help', nil, nil]
|
37
|
+
end
|
38
|
+
|
39
|
+
spec "[!ae733] parses command option definition which has a required param." do
|
40
|
+
sc = @schema
|
41
|
+
tuple = sc.__send__(:parse_optdef, "-f, --file=<FILE>")
|
42
|
+
ok {tuple} == ['f', 'file', '<FILE>', true]
|
43
|
+
tuple = sc.__send__(:parse_optdef, "-f <FILE>")
|
44
|
+
ok {tuple} == ['f', nil, '<FILE>', true]
|
45
|
+
tuple = sc.__send__(:parse_optdef, "--file=<FILE>")
|
46
|
+
ok {tuple} == [nil, 'file', '<FILE>', true]
|
47
|
+
end
|
48
|
+
|
49
|
+
spec "[!4h05c] parses command option definition which has an optional param." do
|
50
|
+
sc = @schema
|
51
|
+
tuple = sc.__send__(:parse_optdef, "-i, --indent[=<WIDTH>]")
|
52
|
+
ok {tuple} == ['i', 'indent', '<WIDTH>', false]
|
53
|
+
tuple = sc.__send__(:parse_optdef, "-i[<WIDTH>]")
|
54
|
+
ok {tuple} == ['i', nil, '<WIDTH>', false]
|
55
|
+
tuple = sc.__send__(:parse_optdef, "--indent[=<WIDTH>]")
|
56
|
+
ok {tuple} == [nil, 'indent', '<WIDTH>', false]
|
57
|
+
end
|
58
|
+
|
59
|
+
spec "[!b7jo3] raises SchemaError when command option definition is invalid." do
|
60
|
+
sc = @schema
|
61
|
+
pr = proc {
|
62
|
+
tuple = sc.__send__(:parse_optdef, "-i, --indent <WIDTH>")
|
63
|
+
}
|
64
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
65
|
+
"-i, --indent <WIDTH>: Invalid option definition (use '=--indent' instead of ' --indent').")
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
topic '#dup()' do
|
72
|
+
|
73
|
+
before do
|
74
|
+
@schema = Benry::CmdOpt::Schema.new
|
75
|
+
@schema.add(:help , "-h" , "print help")
|
76
|
+
end
|
77
|
+
|
78
|
+
spec "[!lxb0o] copies self object." do
|
79
|
+
this = @schema
|
80
|
+
other = @schema.dup()
|
81
|
+
#
|
82
|
+
this_items = this.instance_variable_get(:@items)
|
83
|
+
other_items = other.instance_variable_get(:@items)
|
84
|
+
ok {this_items} != nil
|
85
|
+
ok {other_items} != nil
|
86
|
+
ok {other_items} == this_items
|
87
|
+
ok {other_items.object_id} != this_items.object_id
|
88
|
+
#
|
89
|
+
this.add(:silent, "-s", "silent")
|
90
|
+
other.add(:quiet, "-q", "quiet")
|
91
|
+
ok {this.option_help()} == (" -h : print help\n" +
|
92
|
+
" -s : silent\n")
|
93
|
+
ok {other.option_help()} == (" -h : print help\n" +
|
94
|
+
" -q : quiet\n")
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
topic '#copy_from()' do
|
101
|
+
|
102
|
+
before do
|
103
|
+
@schema1 = Benry::CmdOpt::Schema.new
|
104
|
+
@schema1.add(:help, "-h" , "print help")
|
105
|
+
@schema2 = Benry::CmdOpt::Schema.new
|
106
|
+
@schema2.add(:version, "-v" , "print version")
|
107
|
+
@schema2.add(:debug , "-D" , "debug mode")
|
108
|
+
end
|
109
|
+
|
110
|
+
spec "[!6six3] copy schema items from others." do
|
111
|
+
@schema1.copy_from(@schema2)
|
112
|
+
tuples = @schema1.each.collect {|x| [x.key, x.optdef, x.desc] }
|
113
|
+
ok {tuples} == [
|
114
|
+
[:help , "-h", "print help"],
|
115
|
+
[:version, "-v", "print version"],
|
116
|
+
[:debug , "-D", "debug mode"],
|
117
|
+
]
|
118
|
+
end
|
119
|
+
|
120
|
+
spec "[!vt88s] copy schema items except items specified by 'except:' kwarg." do
|
121
|
+
@schema1.copy_from(@schema2, except: [:debug])
|
122
|
+
tuples = @schema1.each.collect {|x| [x.key, x.optdef, x.desc] }
|
123
|
+
ok {tuples} == [
|
124
|
+
[:help, "-h", "print help"],
|
125
|
+
[:version, "-v", "print version"],
|
126
|
+
]
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
topic '#add()' do
|
133
|
+
|
134
|
+
before do
|
135
|
+
@schema = Benry::CmdOpt::Schema.new
|
136
|
+
end
|
137
|
+
|
138
|
+
spec "[!7hi2d] takes command option definition string." do
|
139
|
+
sc = @schema
|
140
|
+
sc.add(:indent, "-i, --indent=<WIDTH>", "print help")
|
141
|
+
items = sc.instance_eval { @items }
|
142
|
+
ok {items.length} == 1
|
143
|
+
ok {items[0]}.is_a?(Benry::CmdOpt::SchemaItem)
|
144
|
+
ok {items[0].key} == :indent
|
145
|
+
ok {items[0].short} == 'i'
|
146
|
+
ok {items[0].long} == 'indent'
|
147
|
+
ok {items[0].param} == '<WIDTH>'
|
148
|
+
ok {items[0].required?} == true
|
149
|
+
ok {items[0].type} == nil
|
150
|
+
ok {items[0].rexp} == nil
|
151
|
+
ok {items[0].enum} == nil
|
152
|
+
ok {items[0].callback} == nil
|
153
|
+
end
|
154
|
+
|
155
|
+
spec "[!p9924] option key is omittable only when long option specified." do
|
156
|
+
sc = @schema
|
157
|
+
sc.add(nil, "-m, --max-num=<N>", nil)
|
158
|
+
items = sc.instance_eval { @items }
|
159
|
+
ok {items[0].key} == :max_num
|
160
|
+
end
|
161
|
+
|
162
|
+
spec "[!jtp7z] raises SchemaError when key is nil and no long option." do
|
163
|
+
sc = @schema
|
164
|
+
pr = proc { sc.add(nil, "-i <N>", nil) }
|
165
|
+
msg = "add(nil, \"-i <N>\"): Long option required when option key (1st arg) not specified."
|
166
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError, msg)
|
167
|
+
end
|
168
|
+
|
169
|
+
spec "[!rpl98] when long option is 'foo-bar' then key name is ':foo_bar'." do
|
170
|
+
sc = @schema
|
171
|
+
sc.add(nil, "--foo-bar", nil)
|
172
|
+
items = sc.instance_eval { @items }
|
173
|
+
ok {items[0].key} == :foo_bar
|
174
|
+
end
|
175
|
+
|
176
|
+
spec "[!97sn0] raises SchemaError when ',' is missing between short and long options." do
|
177
|
+
sc = @schema
|
178
|
+
pr = proc { sc.add(:exec, '-x --exec=ARG', "exec") }
|
179
|
+
msg = "add(:exec, \"-x --exec=ARG\"): Missing ',' between short option and long options."
|
180
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError, msg)
|
181
|
+
end
|
182
|
+
|
183
|
+
spec "[!yht0v] keeps command option definitions." do
|
184
|
+
sc = @schema
|
185
|
+
sc.add(:indent, "-i, --indent[=<WIDTH>]", "indent width",
|
186
|
+
range: (1..2), value: 8, detail: "(description)", tag: :ab,
|
187
|
+
type: Integer, rexp: /\A\d+\z/, enum: [2, 4, 8]) {|v| v.to_i }
|
188
|
+
items = sc.instance_eval { @items }
|
189
|
+
ok {items.length} == 1
|
190
|
+
ok {items[0]}.is_a?(Benry::CmdOpt::SchemaItem)
|
191
|
+
ok {items[0].key} == :indent
|
192
|
+
ok {items[0].short} == 'i'
|
193
|
+
ok {items[0].long} == 'indent'
|
194
|
+
ok {items[0].param} == '<WIDTH>'
|
195
|
+
ok {items[0].required?} == false
|
196
|
+
ok {items[0].type} == Integer
|
197
|
+
ok {items[0].rexp} == /\A\d+\z/
|
198
|
+
ok {items[0].enum} == [2, 4, 8]
|
199
|
+
ok {items[0].range} == (1..2)
|
200
|
+
ok {items[0].detail} == "(description)"
|
201
|
+
ok {items[0].value} == 8
|
202
|
+
ok {items[0].tag} == :ab
|
203
|
+
ok {items[0].callback}.is_a?(Proc)
|
204
|
+
ok {items[0].callback.arity} == 1
|
205
|
+
end
|
206
|
+
|
207
|
+
spec "[!kuhf9] type, rexp, enum, and range are can be passed as positional args as well as keyword args." do
|
208
|
+
sc = @schema
|
209
|
+
sc.add(:key, "--optdef=xx", "desc", Integer, /\A\d+\z/, [2,4,8], (2..8))
|
210
|
+
item = sc.each.first
|
211
|
+
ok {item.type} == Integer
|
212
|
+
ok {item.rexp} == /\A\d+\z/
|
213
|
+
ok {item.enum} == [2,4,8]
|
214
|
+
ok {item.range} == (2..8)
|
215
|
+
end
|
216
|
+
|
217
|
+
spec "[!e3emy] raises error when positional arg is not one of class, regexp, array, nor range." do
|
218
|
+
sc = @schema
|
219
|
+
pr = proc { sc.add(:key, "--optdef=xx", "desc", "value") }
|
220
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
221
|
+
'"value": Expected one of class, regexp, array or range, but got String.')
|
222
|
+
end
|
223
|
+
|
224
|
+
spec "[!rhhji] raises SchemaError when key is not a Symbol." do
|
225
|
+
sc = @schema
|
226
|
+
pr = proc {
|
227
|
+
sc.add("-i, --indent[=<WIDTH>]", "indent width", nil)
|
228
|
+
}
|
229
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
230
|
+
'add("-i, --indent[=<WIDTH>]", "indent width"): The first arg should be a Symbol as an option key.')
|
231
|
+
end
|
232
|
+
|
233
|
+
spec "[!vq6eq] raises SchemaError when help message is missing." do
|
234
|
+
sc = @schema
|
235
|
+
pr = proc {
|
236
|
+
begin
|
237
|
+
sc.add(:indent, "-i, --indent[=<WIDTH>]", type: Array) # Ruby 2
|
238
|
+
rescue ArgumentError
|
239
|
+
sc.add(:indent, "-i, --indent[=<WIDTH>]", {type: Array}) # Ruby 3
|
240
|
+
end
|
241
|
+
}
|
242
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
243
|
+
'add(:indent, "-i, --indent[=<WIDTH>]"): Help message required as 3rd argument.')
|
244
|
+
end
|
245
|
+
|
246
|
+
|
247
|
+
end
|
248
|
+
|
249
|
+
|
250
|
+
topic '#add_item()' do
|
251
|
+
|
252
|
+
spec "[!qyjp9] raises SchemaError if invalid item added." do
|
253
|
+
sc = Benry::CmdOpt::Schema.new
|
254
|
+
sc.add(:quiet, "-q, --quiet", "quiet mode")
|
255
|
+
#
|
256
|
+
item1 = Benry::CmdOpt::SchemaItem.new(:quiet, "-q", "quiet", "q", nil, nil, false)
|
257
|
+
pr = proc { sc.add_item(item1) }
|
258
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
259
|
+
"quiet: Option key duplicated.")
|
260
|
+
#
|
261
|
+
item2 = Benry::CmdOpt::SchemaItem.new(:quiet2, "-q", "quiet", "q", nil, nil, false)
|
262
|
+
pr = proc { sc.add_item(item2) }
|
263
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
264
|
+
"-q: Short option duplicated (key: quiet2 and quiet).")
|
265
|
+
#
|
266
|
+
item3 = Benry::CmdOpt::SchemaItem.new(:quiet3, "--quiet", "quiet", nil, "quiet", nil, false)
|
267
|
+
pr = proc { sc.add_item(item3) }
|
268
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
269
|
+
"--quiet: Long option duplicated (key: quiet3 and quiet).")
|
270
|
+
end
|
271
|
+
|
272
|
+
spec "[!a693h] adds option item into current schema." do
|
273
|
+
item = Benry::CmdOpt::SchemaItem.new(:quiet, "-q", "quiet", "q", nil, nil, false)
|
274
|
+
sc = Benry::CmdOpt::Schema.new
|
275
|
+
sc.add_item(item)
|
276
|
+
ok {sc.to_s} == <<"END"
|
277
|
+
-q : quiet
|
278
|
+
END
|
279
|
+
end
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
|
284
|
+
topic '#_validate_item()' do
|
285
|
+
|
286
|
+
before do
|
287
|
+
@schema = Benry::CmdOpt::Schema.new
|
288
|
+
@schema.add(:quiet, "-q, --quiet", "quiet mode")
|
289
|
+
end
|
290
|
+
|
291
|
+
spec "[!ewl20] returns error message if option key duplicated." do
|
292
|
+
item = Benry::CmdOpt::SchemaItem.new(:quiet, "-q", "quiet mode", "q", nil, nil, false)
|
293
|
+
ret = @schema.__send__(:_validate_item, item)
|
294
|
+
ok {ret} == "quiet: Option key duplicated."
|
295
|
+
end
|
296
|
+
|
297
|
+
spec "[!xg56v] returns error message if short option duplicated." do
|
298
|
+
item = Benry::CmdOpt::SchemaItem.new(:quiet2, "-q", "quiet mode", "q", nil, nil, false)
|
299
|
+
ret = @schema.__send__(:_validate_item, item)
|
300
|
+
ok {ret} == "-q: Short option duplicated (key: quiet2 and quiet)."
|
301
|
+
end
|
302
|
+
|
303
|
+
spec "[!izezi] returns error message if long option duplicated." do
|
304
|
+
item = Benry::CmdOpt::SchemaItem.new(:quiet3, "--quiet", "quiet mode", nil, "quiet", nil, false)
|
305
|
+
ret = @schema.__send__(:_validate_item, item)
|
306
|
+
ok {ret} == "--quiet: Long option duplicated (key: quiet3 and quiet)."
|
307
|
+
end
|
308
|
+
|
309
|
+
end
|
310
|
+
|
311
|
+
|
312
|
+
topic '#option_help()' do
|
313
|
+
|
314
|
+
before do
|
315
|
+
sc = Benry::CmdOpt::Schema.new
|
316
|
+
sc.add(:help , "-h, --help" , "show help message.")
|
317
|
+
sc.add(:version, " --version" , "print version")
|
318
|
+
sc.add(:file , "-f, --file=<FILE>" , "filename")
|
319
|
+
sc.add(:indent , "-i, --indent[=<WIDTH>]", "enable indent", type: Integer)
|
320
|
+
sc.add(:debug , "-d, --debug" , nil)
|
321
|
+
@schema = sc
|
322
|
+
end
|
323
|
+
|
324
|
+
spec "[!0aq0i] can take integer as width." do
|
325
|
+
helpmsg = @schema.option_help(41)
|
326
|
+
ok {helpmsg} == <<END
|
327
|
+
-h, --help : show help message.
|
328
|
+
--version : print version
|
329
|
+
-f, --file=<FILE> : filename
|
330
|
+
-i, --indent[=<WIDTH>] : enable indent
|
331
|
+
END
|
332
|
+
s = helpmsg.each_line.first.split(':')[0]
|
333
|
+
ok {s.length} == 41+3
|
334
|
+
end
|
335
|
+
|
336
|
+
spec "[!pcsah] can take format string." do
|
337
|
+
helpmsg = @schema.option_help("%-42s: %s")
|
338
|
+
ok {helpmsg} == <<END
|
339
|
+
-h, --help : show help message.
|
340
|
+
--version : print version
|
341
|
+
-f, --file=<FILE> : filename
|
342
|
+
-i, --indent[=<WIDTH>] : enable indent
|
343
|
+
END
|
344
|
+
s = helpmsg.each_line.first.split(':')[0]
|
345
|
+
ok {s.length} == 42+0
|
346
|
+
end
|
347
|
+
|
348
|
+
spec "[!dndpd] detects option width automatically when nothing specified." do
|
349
|
+
helpmsg = @schema.option_help()
|
350
|
+
ok {helpmsg} == <<END
|
351
|
+
-h, --help : show help message.
|
352
|
+
--version : print version
|
353
|
+
-f, --file=<FILE> : filename
|
354
|
+
-i, --indent[=<WIDTH>] : enable indent
|
355
|
+
END
|
356
|
+
s = helpmsg.each_line.to_a.last.split(':')[0]
|
357
|
+
ok {s.length} == 25
|
358
|
+
end
|
359
|
+
|
360
|
+
spec "[!v7z4x] skips option help if help message is not specified." do
|
361
|
+
helpmsg = @schema.option_help()
|
362
|
+
ok {helpmsg} !~ /debug/
|
363
|
+
end
|
364
|
+
|
365
|
+
spec "[!to1th] includes all option help when `all` is true." do
|
366
|
+
helpmsg = @schema.option_help(nil, all: true)
|
367
|
+
ok {helpmsg} =~ /debug/
|
368
|
+
ok {helpmsg} == <<END
|
369
|
+
-h, --help : show help message.
|
370
|
+
--version : print version
|
371
|
+
-f, --file=<FILE> : filename
|
372
|
+
-i, --indent[=<WIDTH>] : enable indent
|
373
|
+
-d, --debug :
|
374
|
+
END
|
375
|
+
end
|
376
|
+
|
377
|
+
spec "[!848rm] supports multi-lines help message." do
|
378
|
+
sc = Benry::CmdOpt::Schema.new
|
379
|
+
sc.add(:mode, "-m, --mode=<MODE>", "output mode",
|
380
|
+
detail: <<"END")
|
381
|
+
v, verbose: print many output
|
382
|
+
q, quiet: print litte output
|
383
|
+
c, compact: print summary output
|
384
|
+
END
|
385
|
+
actual = sc.option_help()
|
386
|
+
expected = <<END
|
387
|
+
-m, --mode=<MODE> : output mode
|
388
|
+
v, verbose: print many output
|
389
|
+
q, quiet: print litte output
|
390
|
+
c, compact: print summary output
|
391
|
+
END
|
392
|
+
ok {actual} == expected
|
393
|
+
end
|
394
|
+
|
395
|
+
spec "[!a4qe4] option should not be hidden if description is empty string." do
|
396
|
+
sc = Benry::CmdOpt::Schema.new
|
397
|
+
sc.add(:debug , "-D", nil) # hidden
|
398
|
+
sc.add(:trace, "-T", "trace", hidden: true) # hidden
|
399
|
+
sc.add(:what , "-W", "") # NOT hidden!
|
400
|
+
ok {sc.option_help()} == <<END
|
401
|
+
-W :
|
402
|
+
END
|
403
|
+
end
|
404
|
+
|
405
|
+
fixture :schema_with_importance do
|
406
|
+
sc = Benry::CmdOpt::Schema.new
|
407
|
+
sc.add(:help , "-h, --help" , "help message")
|
408
|
+
sc.add(:trace , "-T, --trace", "trace" , important: true)
|
409
|
+
sc.add(:debug , "-D, --debug", "debug mode" , important: false)
|
410
|
+
sc.add(:quiet , "-q, --quiet", "quiet mode")
|
411
|
+
sc
|
412
|
+
end
|
413
|
+
|
414
|
+
spec "[!jrwb6] decorates help message according to `important:` value of option." do
|
415
|
+
|schema_with_importance|
|
416
|
+
sc = schema_with_importance
|
417
|
+
ok {sc.option_help()} == <<END
|
418
|
+
-h, --help : help message
|
419
|
+
\e[1m -T, --trace : trace\e[0m
|
420
|
+
\e[2m -D, --debug : debug mode\e[0m
|
421
|
+
-q, --quiet : quiet mode
|
422
|
+
END
|
423
|
+
end
|
424
|
+
|
425
|
+
spec "[!9nlfb] not decorate help message when stdout is not a tty." do
|
426
|
+
|schema_with_importance|
|
427
|
+
sc = schema_with_importance
|
428
|
+
output = nil
|
429
|
+
capture_sio() {
|
430
|
+
ok {$stdout}.NOT.tty?
|
431
|
+
output = sc.option_help()
|
432
|
+
}
|
433
|
+
ok {output} == <<END
|
434
|
+
-h, --help : help message
|
435
|
+
-T, --trace : trace
|
436
|
+
-D, --debug : debug mode
|
437
|
+
-q, --quiet : quiet mode
|
438
|
+
END
|
439
|
+
end
|
440
|
+
|
441
|
+
end
|
442
|
+
|
443
|
+
|
444
|
+
topic '#_default_format()' do
|
445
|
+
|
446
|
+
before do
|
447
|
+
sc = Benry::CmdOpt::Schema.new
|
448
|
+
sc.add(:help , "-h, --help" , "show help message.")
|
449
|
+
sc.add(:version, " --version" , "print version")
|
450
|
+
sc.add(:file , "-f, --file=<FILE>" , "filename")
|
451
|
+
sc.add(:indent , "-i, --indent[=<WIDTH>]", "enable indent", type: Integer)
|
452
|
+
sc.add(:debug , "-d, --debug" , nil)
|
453
|
+
@schema = sc
|
454
|
+
end
|
455
|
+
|
456
|
+
spec "[!kkh9t] returns format string." do
|
457
|
+
ret = @schema.__send__(:_default_format)
|
458
|
+
ok {ret} == " %-22s : %s"
|
459
|
+
end
|
460
|
+
|
461
|
+
spec "[!hr45y] detects preffered option width." do
|
462
|
+
ret = @schema.__send__(:_default_format, 10, 20)
|
463
|
+
ok {ret} == " %-20s : %s"
|
464
|
+
ret = @schema.__send__(:_default_format, 30, 40)
|
465
|
+
ok {ret} == " %-30s : %s"
|
466
|
+
ret = @schema.__send__(:_default_format, 10, 40)
|
467
|
+
ok {ret} == " %-22s : %s"
|
468
|
+
end
|
469
|
+
|
470
|
+
spec "[!bmr7d] changes min_with according to options." do
|
471
|
+
sc = Benry::CmdOpt::Schema.new
|
472
|
+
sc.add(:help , '-h', "help")
|
473
|
+
sc.add(:version, '-v', "version")
|
474
|
+
ok {sc.__send__(:_default_format, nil, 40)} == " %-8s : %s"
|
475
|
+
#
|
476
|
+
sc.add(:file , '-f <FILE>', "filename")
|
477
|
+
ok {sc.__send__(:_default_format, nil, 40)} == " %-14s : %s"
|
478
|
+
#
|
479
|
+
sc.add(:force , '--force', "forcedly")
|
480
|
+
ok {sc.__send__(:_default_format, nil, 40)} == " %-14s : %s"
|
481
|
+
#
|
482
|
+
sc.add(:mode , '-m, --mode=<MODE>', "verbose/quiet")
|
483
|
+
ok {sc.__send__(:_default_format, nil, 40)} == " %-20s : %s"
|
484
|
+
end
|
485
|
+
|
486
|
+
end
|
487
|
+
|
488
|
+
|
489
|
+
topic '#_preferred_option_width()' do
|
490
|
+
|
491
|
+
spec "[!kl91t] shorten option help min width when only single options which take no arg." do
|
492
|
+
sc = Benry::CmdOpt::Schema.new
|
493
|
+
sc.add(:help , '-h', "help")
|
494
|
+
sc.add(:version, '-v', "version")
|
495
|
+
ok {sc.__send__(:_preferred_option_width)} == 8
|
496
|
+
end
|
497
|
+
|
498
|
+
spec "[!0koqb] widen option help min width when any option takes an arg." do
|
499
|
+
sc = Benry::CmdOpt::Schema.new
|
500
|
+
sc.add(:help , '-h', "help")
|
501
|
+
sc.add(:indent , '-i[<N>]', "indent")
|
502
|
+
ok {sc.__send__(:_preferred_option_width)} == 14
|
503
|
+
end
|
504
|
+
|
505
|
+
spec "[!kl91t] widen option help min width when long option exists." do
|
506
|
+
sc = Benry::CmdOpt::Schema.new
|
507
|
+
sc.add(:help , '-h', "help")
|
508
|
+
sc.add(:version, '-v, --version', "version")
|
509
|
+
ok {sc.__send__(:_preferred_option_width)} == 14
|
510
|
+
#
|
511
|
+
sc.add(:file, '--file=<FILE>', "filename")
|
512
|
+
ok {sc.__send__(:_preferred_option_width)} == 20
|
513
|
+
end
|
514
|
+
|
515
|
+
end
|
516
|
+
|
517
|
+
|
518
|
+
topic '#to_s()' do
|
519
|
+
|
520
|
+
spec "[!rrapd] '#to_s' is an alias to '#option_help()'." do
|
521
|
+
schema = Benry::CmdOpt::Schema.new
|
522
|
+
schema.add(:help , "-h, --help" , "show help message")
|
523
|
+
schema.add(:version, " --version" , "print version")
|
524
|
+
ok {schema.to_s} == schema.option_help()
|
525
|
+
end
|
526
|
+
|
527
|
+
end
|
528
|
+
|
529
|
+
|
530
|
+
topic '#each_option_and_desc()' do
|
531
|
+
|
532
|
+
before do
|
533
|
+
sc = Benry::CmdOpt::Schema.new
|
534
|
+
sc.add(:help, "-h, --help", "show help message")
|
535
|
+
sc.add(:version, " --version", "print version")
|
536
|
+
sc.add(:debug , "-d, --debug" , nil) # hidden
|
537
|
+
sc.add(:DEBUG , "-D, --DEBUG" , "debug mode", hidden: true) # hidden
|
538
|
+
@schema = sc
|
539
|
+
end
|
540
|
+
|
541
|
+
spec "[!4b911] yields each optin definition str and help message." do
|
542
|
+
pairs = []
|
543
|
+
@schema.each_option_and_desc {|opt, desc| pairs << [opt, desc] }
|
544
|
+
ok {pairs} == [
|
545
|
+
["-h, --help" , "show help message"], # not hiddden
|
546
|
+
[" --version" , "print version"], # not hidden
|
547
|
+
]
|
548
|
+
end
|
549
|
+
|
550
|
+
spec "[!cl8zy] when 'all' flag is false, not yield hidden items." do
|
551
|
+
pairs = []
|
552
|
+
@schema.each_option_and_desc(all: false) {|opt, desc| pairs << [opt, desc] }
|
553
|
+
ok {pairs} == [
|
554
|
+
["-h, --help" , "show help message"], # not hiddden
|
555
|
+
[" --version" , "print version"], # not hidden
|
556
|
+
]
|
557
|
+
end
|
558
|
+
|
559
|
+
spec "[!tc4bk] when 'all' flag is true, yields even hidden items." do
|
560
|
+
pairs = []
|
561
|
+
@schema.each_option_and_desc(all: true) {|opt, desc| pairs << [opt, desc] }
|
562
|
+
ok {pairs} == [
|
563
|
+
["-h, --help" , "show help message"], # not hiddden
|
564
|
+
[" --version" , "print version"], # not hidden
|
565
|
+
["-d, --debug" , nil], # hidden
|
566
|
+
["-D, --DEBUG" , "debug mode"], # hidden
|
567
|
+
]
|
568
|
+
end
|
569
|
+
|
570
|
+
spec "[!03sux] returns enumerator object if block not given." do
|
571
|
+
## when 'all: true'
|
572
|
+
xs = @schema.each_option_and_desc(all: true)
|
573
|
+
ok {xs}.is_a?(Enumerator)
|
574
|
+
ok {xs.collect {|x, _| x }} == ["-h, --help", " --version", "-d, --debug", "-D, --DEBUG"]
|
575
|
+
## when 'all: false'
|
576
|
+
xs = @schema.each_option_and_desc(all: false)
|
577
|
+
ok {xs}.is_a?(Enumerator)
|
578
|
+
ok {xs.collect {|x, _| x }} == ["-h, --help", " --version"]
|
579
|
+
end
|
580
|
+
|
581
|
+
spec "[!zbxyv] returns self." do
|
582
|
+
ret = @schema.each_option_and_desc { nil }
|
583
|
+
ok {ret}.same? @schema
|
584
|
+
end
|
585
|
+
|
586
|
+
end
|
587
|
+
|
588
|
+
|
589
|
+
topic '#each()' do
|
590
|
+
|
591
|
+
before do
|
592
|
+
@schema = Benry::CmdOpt::Schema.new
|
593
|
+
@schema.add(:help , "-h, --help" , "help message")
|
594
|
+
@schema.add(:version, " --version", "print version")
|
595
|
+
end
|
596
|
+
|
597
|
+
spec "[!y4k1c] yields each option item." do
|
598
|
+
items = []
|
599
|
+
@schema.each {|x| items << x }
|
600
|
+
ok {items.length} == 2
|
601
|
+
ok {items[0]}.is_a?(Benry::CmdOpt::SchemaItem)
|
602
|
+
ok {items[1]}.is_a?(Benry::CmdOpt::SchemaItem)
|
603
|
+
ok {items[0].key} == :help
|
604
|
+
ok {items[1].key} == :version
|
605
|
+
keys = @schema.each.collect {|x| x.key }
|
606
|
+
ok {keys} == [:help, :version]
|
607
|
+
end
|
608
|
+
|
609
|
+
end
|
610
|
+
|
611
|
+
|
612
|
+
topic '#empty?()' do
|
613
|
+
|
614
|
+
spec "[!um8am] returns false if any item exists, else returns true." do
|
615
|
+
schema = Benry::CmdOpt::Schema.new
|
616
|
+
ok {schema.empty?()} == true
|
617
|
+
schema.add(:help , "-h, --help" , "help message")
|
618
|
+
ok {schema.empty?()} == false
|
619
|
+
end
|
620
|
+
|
621
|
+
spec "[!icvm1] ignores hidden items if 'all: false' kwarg specified." do
|
622
|
+
schema = Benry::CmdOpt::Schema.new
|
623
|
+
schema.add(:debug , "-D", nil)
|
624
|
+
schema.add(:trace, "-T", "trace", hidden: true)
|
625
|
+
ok {schema.empty?()} == false
|
626
|
+
ok {schema.empty?(all: true)} == false
|
627
|
+
ok {schema.empty?(all: false)} == true
|
628
|
+
end
|
629
|
+
|
630
|
+
end
|
631
|
+
|
632
|
+
|
633
|
+
topic '#get()' do
|
634
|
+
|
635
|
+
before do
|
636
|
+
@schema = Benry::CmdOpt::Schema.new
|
637
|
+
@schema.add(:help , "-h, --help" , "help message")
|
638
|
+
@schema.add(:version, " --version", "print version")
|
639
|
+
end
|
640
|
+
|
641
|
+
spec "[!3wjfp] finds option item object by key." do
|
642
|
+
item = @schema.get(:help)
|
643
|
+
ok {item.key} == :help
|
644
|
+
item = @schema.get(:version)
|
645
|
+
ok {item.key} == :version
|
646
|
+
end
|
647
|
+
|
648
|
+
spec "[!0spll] returns nil if key not found." do
|
649
|
+
ok {@schema.get(:debug)} == nil
|
650
|
+
end
|
651
|
+
|
652
|
+
end
|
653
|
+
|
654
|
+
|
655
|
+
topic '#delete()' do
|
656
|
+
|
657
|
+
before do
|
658
|
+
@schema = Benry::CmdOpt::Schema.new
|
659
|
+
@schema.add(:help , "-h, --help" , "help message")
|
660
|
+
@schema.add(:version, " --version", "print version")
|
661
|
+
end
|
662
|
+
|
663
|
+
spec "[!l86rb] deletes option item corresponding to key." do
|
664
|
+
keys = @schema.each.collect {|x| x.key }
|
665
|
+
ok {keys} == [:help, :version]
|
666
|
+
@schema.delete(:help)
|
667
|
+
keys = @schema.each.collect {|x| x.key }
|
668
|
+
ok {keys} == [:version]
|
669
|
+
end
|
670
|
+
|
671
|
+
spec "[!rq0aa] returns deleted item." do
|
672
|
+
item = @schema.delete(:version)
|
673
|
+
ok {item} != nil
|
674
|
+
ok {item.key} == :version
|
675
|
+
end
|
676
|
+
|
677
|
+
end
|
678
|
+
|
679
|
+
|
680
|
+
topic '#find_short_option()' do
|
681
|
+
|
682
|
+
before do
|
683
|
+
@schema = new_test_schema()
|
684
|
+
end
|
685
|
+
|
686
|
+
spec "[!b4js1] returns option definition matched to short name." do
|
687
|
+
x = @schema.find_short_option('h')
|
688
|
+
ok {x.key} == :help
|
689
|
+
x = @schema.find_short_option('f')
|
690
|
+
ok {x.key} == :file
|
691
|
+
x = @schema.find_short_option('i')
|
692
|
+
ok {x.key} == :indent
|
693
|
+
end
|
694
|
+
|
695
|
+
spec "[!s4d1y] returns nil when nothing found." do
|
696
|
+
ok {@schema.find_short_option('v')} == nil
|
697
|
+
end
|
698
|
+
|
699
|
+
end
|
700
|
+
|
701
|
+
|
702
|
+
topic '#find_long_option()' do
|
703
|
+
|
704
|
+
before do
|
705
|
+
@schema = new_test_schema()
|
706
|
+
end
|
707
|
+
|
708
|
+
spec "[!atmf9] returns option definition matched to long name." do
|
709
|
+
x = @schema.find_long_option('help')
|
710
|
+
ok {x.key} == :help
|
711
|
+
x = @schema.find_long_option('file')
|
712
|
+
ok {x.key} == :file
|
713
|
+
x = @schema.find_long_option('indent')
|
714
|
+
ok {x.key} == :indent
|
715
|
+
end
|
716
|
+
|
717
|
+
spec "[!6haoo] returns nil when nothing found." do
|
718
|
+
ok {@schema.find_long_option('lib')} == nil
|
719
|
+
end
|
720
|
+
|
721
|
+
end
|
722
|
+
|
723
|
+
|
724
|
+
end
|
725
|
+
|
726
|
+
|
727
|
+
end
|