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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bfcbb37a1e33df36573f607fc8a5b7f81761b0535422023b64648ac596b1ce4c
4
- data.tar.gz: d9b57e362e8d3e6ec7bf808b661436cbe827b3f5ba7d87f6ea5c55c0bafce69c
3
+ metadata.gz: cb625857a40a61b165810acf4ee25cb3c9db54ac6e2c9e14678206a1036817a5
4
+ data.tar.gz: a47a9b936e4d63ea088945498d9469af67855af74c39680ae909ef36a6292945
5
5
  SHA512:
6
- metadata.gz: 1710700fceb026a2fe422ccaf5681dd83e4160a25c9f2fc3bc35662cfb695b7588fb2d9eea0ec7211c65a6e297bb3675e7dd3af63cfc9eaa4eb05561dc9714ca
7
- data.tar.gz: 43c560b46bb5de501f5d82442092644961d711a46770161bcebb0bd8fcebfb5fabf15add17f5f875c32f66a07d8bdb6ce45eab3bf493ce54f64336add6cd0817
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.0 $)
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.0.0) contains only 429 lines (except both, too).
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(:lib , '-I <NAME>', "library name") {|options, key, val|
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", "-Ibaz"])
483
- p options #=> {:lib=>["foo", "bar", "baz"]}
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "benry-cmdopt"
5
- spec.version = "$Release: 2.3.0 $".split()[1]
5
+ spec.version = "$Release: 2.4.0 $".split()[1]
6
6
  spec.author = "kwatch"
7
7
  spec.email = "kwatch@gmail.com"
8
8
  spec.platform = Gem::Platform::RUBY
@@ -21,7 +21,7 @@
21
21
  <ul class="nav">
22
22
  </ul>
23
23
  </nav>
24
- <p>($Release: 2.3.0 $)</p>
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.0.0) contains only 429 lines (except both, too).</li>
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] #=&gt; <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(:lib , '-I &ltNAME&gt;', "library name") <strong>{|options, key, val|</strong>
473
+ cmdopt.add(:inc , '-I &ltpath&gt;', "include path", <strong>multiple: true</strong>) # !!!!
474
+ options = cmdopt.parse(["-I", "/foo", "-I", "/bar", "-I/baz"])
475
+ p options #=&gt; <strong>{:inc=&gt;["/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 &ltpath&gt;', "include path") <strong>{|options, key, val|</strong>
468
483
  <strong>arr = options[key] || []</strong>
469
484
  <strong>arr &lt&lt val</strong>
470
485
  <strong>arr</strong>
@@ -472,8 +487,8 @@ cmdopt.add(:lib , '-I &ltNAME&gt;', "library name") <strong>{|options, key, val|
472
487
  #(options[key] || []) &lt&lt val
473
488
  <strong>}</strong>
474
489
 
475
- options = cmdopt.parse(["-I", "foo", "-I", "bar", "-Ibaz"])
476
- p options #=&gt; <strong>{:lib=&gt;["foo", "bar", "baz"]}</strong>
490
+ options = cmdopt.parse(["-I", "/foo", "-I", "/bar", "-I/baz"])
491
+ p options #=&gt; <strong>{:inc=&gt;["/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"]) #=&gt; {:verbose=&gt;1}
715
+ p cmdopt.parse(["-vv"]) #=&gt; {:verbose=&gt;2}
716
+ p cmdopt.parse(["-vvv"]) #=&gt; {:verbose=&gt;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.3.0 $
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.3.0 $'.split()[1]
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, type: type, rexp: rexp, enum: enum, range: range, value: value, detail: detail, hidden: hidden, important: important, tag: tag, &callback)
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, detail: nil, value: nil, hidden: nil, important: nil, tag: nil, &callback)
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, :detail, :value, :tag, :callback
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
- optdict[item.key] = val
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
- optdict[item.key] = val
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.")