benry-cmdopt 2.3.0 → 2.4.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/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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb625857a40a61b165810acf4ee25cb3c9db54ac6e2c9e14678206a1036817a5
|
4
|
+
data.tar.gz: a47a9b936e4d63ea088945498d9469af67855af74c39680ae909ef36a6292945
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a35732c27d59d58ab02e23e22b7b52750975a728988b3f78c0d26bdcf52cfaffb7caa8d1feb2e1928a3455220e44dca0673720072dc31f164a87c08b6ed637c
|
7
|
+
data.tar.gz: 8ad1c06cb5b9e5adeb84fc01c4c236162a894c3fbea1f9470dbc87d67bb315c8669f8c84036cc1e594ac22f7047d5bb10fa790e298b24624d26858cc2ff3e127
|
data/CHANGES.md
CHANGED
@@ -2,6 +2,12 @@ CHANGES
|
|
2
2
|
=======
|
3
3
|
|
4
4
|
|
5
|
+
Release 2.4.0 (2023-12-04)
|
6
|
+
--------------------------
|
7
|
+
|
8
|
+
* [enhance] `Schema.add()` supports `multiple: true` keyword arg which enables to specify option multiple times.
|
9
|
+
|
10
|
+
|
5
11
|
Release 2.3.0 (2023-11-26)
|
6
12
|
--------------------------
|
7
13
|
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Benry-CmdOpt
|
2
2
|
|
3
|
-
($Release: 2.
|
3
|
+
($Release: 2.4.0 $)
|
4
4
|
|
5
5
|
|
6
6
|
|
@@ -41,6 +41,9 @@ Benry-CmdOpt requires Ruby >= 2.3.
|
|
41
41
|
* [Important Options](#important-options)
|
42
42
|
* [Not Supported](#not-supported)
|
43
43
|
* [Internal Classes](#internal-classes)
|
44
|
+
* [FAQ](#faq)
|
45
|
+
* [Q: How to change or customize error messages?](#q-how-to-change-or-customize-error-messages)
|
46
|
+
* [Q: Is it possible to support `-vvv` style option?](#q-is-it-possible-to-support--vvv-style-option)
|
44
47
|
* [License and Copyright](#license-and-copyright)
|
45
48
|
|
46
49
|
<!-- /TOC -->
|
@@ -277,13 +280,13 @@ end
|
|
277
280
|
|
278
281
|
In fact, file `optparse.rb` and `optparse/*.rb` (in Ruby 3.2)
|
279
282
|
contains total 1298 lines (except comments and blanks), while
|
280
|
-
`benry/cmdopt.rb` (v2.
|
283
|
+
`benry/cmdopt.rb` (v2.4.0) contains only 479 lines (except both, too).
|
281
284
|
|
282
285
|
|
283
286
|
|
284
287
|
## Install
|
285
288
|
|
286
|
-
```
|
289
|
+
```console
|
287
290
|
$ gem install benry-cmdopt
|
288
291
|
```
|
289
292
|
|
@@ -467,11 +470,24 @@ p options[:flag2] #=> false (!!!!!)
|
|
467
470
|
|
468
471
|
### Multiple Value Option
|
469
472
|
|
473
|
+
Release 2.4 or later supports `multiple: true` keyword arg.
|
474
|
+
|
470
475
|
```ruby
|
471
476
|
require 'benry/cmdopt'
|
472
477
|
cmdopt = Benry::CmdOpt.new
|
473
478
|
|
474
|
-
cmdopt.add(:
|
479
|
+
cmdopt.add(:inc , '-I <path>', "include path", multiple: true) # !!!!
|
480
|
+
options = cmdopt.parse(["-I", "/foo", "-I", "/bar", "-I/baz"])
|
481
|
+
p options #=> {:inc=>["/foo", "/bar", "/baz"]}
|
482
|
+
```
|
483
|
+
|
484
|
+
On older version:
|
485
|
+
|
486
|
+
```ruby
|
487
|
+
require 'benry/cmdopt'
|
488
|
+
cmdopt = Benry::CmdOpt.new
|
489
|
+
|
490
|
+
cmdopt.add(:inc , '-I <path>', "include path") {|options, key, val|
|
475
491
|
arr = options[key] || []
|
476
492
|
arr << val
|
477
493
|
arr
|
@@ -479,8 +495,8 @@ cmdopt.add(:lib , '-I <NAME>', "library name") {|options, key, val|
|
|
479
495
|
#(options[key] || []) << val
|
480
496
|
}
|
481
497
|
|
482
|
-
options = cmdopt.parse(["-I", "foo", "-I", "bar", "-
|
483
|
-
p options #=> {:
|
498
|
+
options = cmdopt.parse(["-I", "/foo", "-I", "/bar", "-I/baz"])
|
499
|
+
p options #=> {:inc=>["/foo", "/bar", "/baz"]}
|
484
500
|
```
|
485
501
|
|
486
502
|
|
@@ -704,6 +720,34 @@ Use `cmdopt.is_a?(Benry::CmdOpt::Facade)` instead if necessary.
|
|
704
720
|
|
705
721
|
|
706
722
|
|
723
|
+
## FAQ
|
724
|
+
|
725
|
+
|
726
|
+
### Q: How to change or customize error messages?
|
727
|
+
|
728
|
+
A: Currently not supported. Maybe supported in the future.
|
729
|
+
|
730
|
+
|
731
|
+
### Q: Is it possible to support `-vvv` style option?
|
732
|
+
|
733
|
+
A: Yes.
|
734
|
+
|
735
|
+
```ruby
|
736
|
+
require 'benry/cmdopt'
|
737
|
+
cmdopt = Benry::CmdOpt.new
|
738
|
+
|
739
|
+
cmdopt.add(:verbose , '-v', "verbose level") {|opts, key, val|
|
740
|
+
opts[key] ||= 0
|
741
|
+
opts[key] += 1
|
742
|
+
}
|
743
|
+
|
744
|
+
p cmdopt.parse(["-v"]) #=> {:verbose=>1}
|
745
|
+
p cmdopt.parse(["-vv"]) #=> {:verbose=>2}
|
746
|
+
p cmdopt.parse(["-vvv"]) #=> {:verbose=>3}
|
747
|
+
```
|
748
|
+
|
749
|
+
|
750
|
+
|
707
751
|
## License and Copyright
|
708
752
|
|
709
753
|
$License: MIT License $
|
data/benry-cmdopt.gemspec
CHANGED
data/doc/benry-cmdopt.html
CHANGED
@@ -21,7 +21,7 @@
|
|
21
21
|
<ul class="nav">
|
22
22
|
</ul>
|
23
23
|
</nav>
|
24
|
-
<p>($Release: 2.
|
24
|
+
<p>($Release: 2.4.0 $)</p>
|
25
25
|
<section class="section" id="whats-this">
|
26
26
|
<h2>What's This?</h2>
|
27
27
|
<p>Benry-CmdOpt is a command option parser library, like <code>optparse.rb</code>
|
@@ -57,6 +57,11 @@ and easy to understahnd.</p>
|
|
57
57
|
<li><a href="#not-supported">Not Supported</a></li>
|
58
58
|
</ul></li>
|
59
59
|
<li><a href="#internal-classes">Internal Classes</a></li>
|
60
|
+
<li><a href="#faq">FAQ</a>
|
61
|
+
<ul>
|
62
|
+
<li><a href="#q-how-to-change-or-customize-error-messages">Q: How to change or customize error messages?</a></li>
|
63
|
+
<li><a href="#q-is-it-possible-to-support--vvv-style-option">Q: Is it possible to support <code>-vvv</code> style option?</a></li>
|
64
|
+
</ul></li>
|
60
65
|
<li><a href="#license-and-copyright">License and Copyright</a></li>
|
61
66
|
</ul>
|
62
67
|
</div>
|
@@ -293,12 +298,12 @@ end
|
|
293
298
|
<p></p>
|
294
299
|
In fact, file <code>optparse.rb</code> and <code>optparse/*.rb</code> (in Ruby 3.2)
|
295
300
|
contains total 1298 lines (except comments and blanks), while
|
296
|
-
<code>benry/cmdopt.rb</code> (v2.
|
301
|
+
<code>benry/cmdopt.rb</code> (v2.4.0) contains only 479 lines (except both, too).</li>
|
297
302
|
</ul>
|
298
303
|
</section>
|
299
304
|
<section class="section" id="install">
|
300
305
|
<h2>Install</h2>
|
301
|
-
<pre>
|
306
|
+
<pre class="language-console">
|
302
307
|
$ gem install benry-cmdopt
|
303
308
|
</pre>
|
304
309
|
</section>
|
@@ -460,11 +465,21 @@ p options[:flag2] #=> <strong>false</strong> (!!!!!)
|
|
460
465
|
</section>
|
461
466
|
<section class="subsection" id="multiple-value-option">
|
462
467
|
<h3>Multiple Value Option</h3>
|
468
|
+
<p>Release 2.4 or later supports <code>multiple: true</code> keyword arg.</p>
|
463
469
|
<pre class="language-ruby">
|
464
470
|
require 'benry/cmdopt'
|
465
471
|
cmdopt = Benry::CmdOpt.new
|
466
472
|
|
467
|
-
cmdopt.add(:
|
473
|
+
cmdopt.add(:inc , '-I <path>', "include path", <strong>multiple: true</strong>) # !!!!
|
474
|
+
options = cmdopt.parse(["-I", "/foo", "-I", "/bar", "-I/baz"])
|
475
|
+
p options #=> <strong>{:inc=>["/foo", "/bar", "/baz"]}</strong>
|
476
|
+
</pre>
|
477
|
+
<p>On older version:</p>
|
478
|
+
<pre class="language-ruby">
|
479
|
+
require 'benry/cmdopt'
|
480
|
+
cmdopt = Benry::CmdOpt.new
|
481
|
+
|
482
|
+
cmdopt.add(:inc , '-I <path>', "include path") <strong>{|options, key, val|</strong>
|
468
483
|
<strong>arr = options[key] || []</strong>
|
469
484
|
<strong>arr << val</strong>
|
470
485
|
<strong>arr</strong>
|
@@ -472,8 +487,8 @@ cmdopt.add(:lib , '-I <NAME>', "library name") <strong>{|options, key, val|
|
|
472
487
|
#(options[key] || []) << val
|
473
488
|
<strong>}</strong>
|
474
489
|
|
475
|
-
options = cmdopt.parse(["-I", "foo", "-I", "bar", "-
|
476
|
-
p options #=> <strong>{:
|
490
|
+
options = cmdopt.parse(["-I", "/foo", "-I", "/bar", "-I/baz"])
|
491
|
+
p options #=> <strong>{:inc=>["/foo", "/bar", "/baz"]}</strong>
|
477
492
|
</pre>
|
478
493
|
</section>
|
479
494
|
<section class="subsection" id="hidden-option">
|
@@ -678,6 +693,30 @@ opts = <strong>cmdopt.parse</strong>(ARGV) # same as <strong>pars
|
|
678
693
|
<p>Notice that <code>cmdopt.is_a?(Benry::CmdOpt)</code> results in false.
|
679
694
|
Use <code>cmdopt.is_a?(Benry::CmdOpt::Facade)</code> instead if necessary.</p>
|
680
695
|
</section>
|
696
|
+
<section class="section" id="faq">
|
697
|
+
<h2>FAQ</h2>
|
698
|
+
<section class="subsection" id="q-how-to-change-or-customize-error-messages">
|
699
|
+
<h3>Q: How to change or customize error messages?</h3>
|
700
|
+
<p>A: Currently not supported. Maybe supported in the future.</p>
|
701
|
+
</section>
|
702
|
+
<section class="subsection" id="q-is-it-possible-to-support--vvv-style-option">
|
703
|
+
<h3>Q: Is it possible to support <code>-vvv</code> style option?</h3>
|
704
|
+
<p>A: Yes.</p>
|
705
|
+
<pre class="language-ruby">
|
706
|
+
require 'benry/cmdopt'
|
707
|
+
cmdopt = Benry::CmdOpt.new
|
708
|
+
|
709
|
+
cmdopt.add(:verbose , '-v', "verbose level") {|opts, key, val|
|
710
|
+
opts[key] ||= 0
|
711
|
+
opts[key] += 1
|
712
|
+
}
|
713
|
+
|
714
|
+
p cmdopt.parse(["-v"]) #=> {:verbose=>1}
|
715
|
+
p cmdopt.parse(["-vv"]) #=> {:verbose=>2}
|
716
|
+
p cmdopt.parse(["-vvv"]) #=> {:verbose=>3}
|
717
|
+
</pre>
|
718
|
+
</section>
|
719
|
+
</section>
|
681
720
|
<section class="section" id="license-and-copyright">
|
682
721
|
<h2>License and Copyright</h2>
|
683
722
|
<p>$License: MIT License $</p>
|
data/lib/benry/cmdopt.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
###
|
5
|
-
### $Release: 2.
|
5
|
+
### $Release: 2.4.0 $
|
6
6
|
### $Copyright: copyright(c) 2021 kwatch@gmail.com $
|
7
7
|
### $License: MIT License $
|
8
8
|
###
|
@@ -23,7 +23,7 @@ end
|
|
23
23
|
module Benry::CmdOpt
|
24
24
|
|
25
25
|
|
26
|
-
VERSION = '$Release: 2.
|
26
|
+
VERSION = '$Release: 2.4.0 $'.split()[1]
|
27
27
|
|
28
28
|
|
29
29
|
def self.new()
|
@@ -40,11 +40,12 @@ module Benry::CmdOpt
|
|
40
40
|
|
41
41
|
attr_reader :schema
|
42
42
|
|
43
|
-
def add(key, optdef, desc, *rest, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, value: nil, detail: nil, hidden: nil, important: nil, tag: nil, &callback)
|
43
|
+
def add(key, optdef, desc, *rest, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, value: nil, multiple: nil, detail: nil, hidden: nil, important: nil, tag: nil, &callback)
|
44
44
|
rexp ||= pattern # for backward compatibility
|
45
45
|
#; [!vmb3r] defines command option.
|
46
46
|
#; [!71cvg] type, rexp, enum, and range are can be passed as positional args as well as keyword args.
|
47
|
-
@schema.add(key, optdef, desc, *rest,
|
47
|
+
@schema.add(key, optdef, desc, *rest,
|
48
|
+
type: type, rexp: rexp, enum: enum, range: range, value: value, multiple: multiple, detail: detail, hidden: hidden, important: important, tag: tag, &callback)
|
48
49
|
#; [!tu4k3] returns self.
|
49
50
|
self
|
50
51
|
end
|
@@ -103,7 +104,7 @@ module Benry::CmdOpt
|
|
103
104
|
self
|
104
105
|
end
|
105
106
|
|
106
|
-
def add(key, optdef, desc, *rest, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, value: nil, detail: nil, hidden: nil, important: nil, tag: nil, &callback)
|
107
|
+
def add(key, optdef, desc, *rest, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, value: nil, multiple: nil, detail: nil, hidden: nil, important: nil, tag: nil, &callback)
|
107
108
|
rexp ||= pattern # for backward compatibility
|
108
109
|
#; [!kuhf9] type, rexp, enum, and range are can be passed as positional args as well as keyword args.
|
109
110
|
rest.each do |x|
|
@@ -137,7 +138,7 @@ module Benry::CmdOpt
|
|
137
138
|
end
|
138
139
|
#; [!yht0v] keeps command option definitions.
|
139
140
|
item = SchemaItem.new(key, optdef, desc, short, long, param, required,
|
140
|
-
type: type, rexp: rexp, enum: enum, range: range, value: value, detail: detail, hidden: hidden, important: important, tag: tag, &callback)
|
141
|
+
type: type, rexp: rexp, enum: enum, range: range, value: value, multiple: multiple, detail: detail, hidden: hidden, important: important, tag: tag, &callback)
|
141
142
|
add_item(item)
|
142
143
|
item
|
143
144
|
end
|
@@ -322,7 +323,7 @@ module Benry::CmdOpt
|
|
322
323
|
|
323
324
|
class SchemaItem # avoid Struct
|
324
325
|
|
325
|
-
def initialize(key, optdef, desc, short, long, param, required, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil,
|
326
|
+
def initialize(key, optdef, desc, short, long, param, required, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, value: nil, multiple: nil, detail: nil, hidden: nil, important: nil, tag: nil, &callback)
|
326
327
|
rexp ||= pattern # for backward compatibility
|
327
328
|
_init_validation(param, required, type, rexp, enum, range, value)
|
328
329
|
@key = key unless nil == key
|
@@ -336,8 +337,9 @@ module Benry::CmdOpt
|
|
336
337
|
@rexp = rexp unless nil == rexp
|
337
338
|
@enum = enum unless nil == enum
|
338
339
|
@range = range unless nil == range
|
339
|
-
@detail = detail unless nil == detail
|
340
340
|
@value = value unless nil == value
|
341
|
+
@multiple = multiple unless nil == multiple
|
342
|
+
@detail = detail unless nil == detail
|
341
343
|
@hidden = hidden unless nil == hidden
|
342
344
|
@important = important unless nil == important
|
343
345
|
@tag = tag unless nil == tag
|
@@ -346,7 +348,7 @@ module Benry::CmdOpt
|
|
346
348
|
@enum.freeze() if @enum
|
347
349
|
end
|
348
350
|
|
349
|
-
attr_reader :key, :optdef, :desc, :short, :long, :param, :type, :rexp, :enum, :range, :
|
351
|
+
attr_reader :key, :optdef, :desc, :short, :long, :param, :type, :rexp, :enum, :range, :value, :detail, :tag, :callback
|
350
352
|
attr_writer :desc, :detail, :hidden, :important, :tag # !!experimental!!
|
351
353
|
alias pattern rexp # for backward compatibility
|
352
354
|
alias help desc # for backward compatibility
|
@@ -367,6 +369,12 @@ module Benry::CmdOpt
|
|
367
369
|
return :optional
|
368
370
|
end
|
369
371
|
|
372
|
+
def multiple?()
|
373
|
+
#; [!1lj8v] returns true if @multiple is truthy.
|
374
|
+
#; [!cun23] returns false if @multiple is falthy.
|
375
|
+
return !! @multiple
|
376
|
+
end
|
377
|
+
|
370
378
|
def hidden?()
|
371
379
|
#; [!no6ov] returns true if @hidden is true.
|
372
380
|
#; [!ej8ot] returns false if @hidden is false.
|
@@ -629,7 +637,8 @@ module Benry::CmdOpt
|
|
629
637
|
rescue RuntimeError => ex
|
630
638
|
raise _error("#{optstr}: #{ex.message}")
|
631
639
|
end
|
632
|
-
|
640
|
+
#; [!1m87b] supports multiple option.
|
641
|
+
store_option_value(optdict, item, val)
|
633
642
|
end
|
634
643
|
|
635
644
|
def parse_short_options(optstr, optdict, &block)
|
@@ -669,7 +678,8 @@ module Benry::CmdOpt
|
|
669
678
|
raise _error("-#{char}#{sp}#{val}: #{ex.message}")
|
670
679
|
end
|
671
680
|
end
|
672
|
-
|
681
|
+
#; [!187r2] supports multiple option.
|
682
|
+
store_option_value(optdict, item, val)
|
673
683
|
end
|
674
684
|
end
|
675
685
|
|
@@ -678,6 +688,16 @@ module Benry::CmdOpt
|
|
678
688
|
return OPTIONS_CLASS.new
|
679
689
|
end
|
680
690
|
|
691
|
+
def store_option_value(optdict, item, val)
|
692
|
+
#; [!my86j] stores multiple values if multiple option item.
|
693
|
+
if item.multiple?
|
694
|
+
(optdict[item.key] ||= []) << val
|
695
|
+
#; [!tm7xw] stores singile value if not multiple option item.
|
696
|
+
else
|
697
|
+
optdict[item.key] = val
|
698
|
+
end
|
699
|
+
end
|
700
|
+
|
681
701
|
def handle_unknown_long_option(optstr, name, val)
|
682
702
|
#; [!0q78a] raises OptionError.
|
683
703
|
raise _error("#{optstr}: Unknown long option.")
|