benry-cmdopt 2.2.0 → 2.3.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 +7 -1
- data/README.md +18 -1
- data/benry-cmdopt.gemspec +1 -1
- data/doc/benry-cmdopt.html +18 -1
- data/doc/css/style.css +2 -2
- data/lib/benry/cmdopt.rb +24 -3
- data/test/cmdopt_test.rb +52 -4
- 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: bfcbb37a1e33df36573f607fc8a5b7f81761b0535422023b64648ac596b1ce4c
|
4
|
+
data.tar.gz: d9b57e362e8d3e6ec7bf808b661436cbe827b3f5ba7d87f6ea5c55c0bafce69c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1710700fceb026a2fe422ccaf5681dd83e4160a25c9f2fc3bc35662cfb695b7588fb2d9eea0ec7211c65a6e297bb3675e7dd3af63cfc9eaa4eb05561dc9714ca
|
7
|
+
data.tar.gz: 43c560b46bb5de501f5d82442092644961d711a46770161bcebb0bd8fcebfb5fabf15add17f5f875c32f66a07d8bdb6ce45eab3bf493ce54f64336add6cd0817
|
data/CHANGES.md
CHANGED
@@ -2,10 +2,16 @@ CHANGES
|
|
2
2
|
=======
|
3
3
|
|
4
4
|
|
5
|
+
Release 2.3.0 (2023-11-26)
|
6
|
+
--------------------------
|
7
|
+
|
8
|
+
* [change] `Schema#add()` raises SchemaError if option key, short or long option duplicated.
|
9
|
+
|
10
|
+
|
5
11
|
Release 2.2.0 (2023-10-28)
|
6
12
|
--------------------------
|
7
13
|
|
8
|
-
* [enhance] Define `
|
14
|
+
* [enhance] Define `Schema#add_item()` which adds option item into the schema. This is for Benry-CmdApp framework.
|
9
15
|
* [change] (EXPERIMENTAL) Make `desc`, `detail`, `hidden`, `important`, `tag` attrubtes of `SchemaItem` object to be writable. This is for Benry-CmdApp framework.
|
10
16
|
|
11
17
|
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Benry-CmdOpt
|
2
2
|
|
3
|
-
($Release: 2.
|
3
|
+
($Release: 2.3.0 $)
|
4
4
|
|
5
5
|
|
6
6
|
|
@@ -139,6 +139,23 @@ cmdopt = Benry::CmdOpt.new
|
|
139
139
|
cmdopt.add(:number, "-n <N>", "number", type: Integer, enum: [1, 2, 3]) # very intuitive
|
140
140
|
```
|
141
141
|
|
142
|
+
* `optparse.rb` doesn't report error even when options are duplicated.
|
143
|
+
This specification makes debugging hard.
|
144
|
+
|
145
|
+
`benry/cmdopt.rb` reports error when options are duplicated.
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
require 'optparse'
|
149
|
+
|
150
|
+
options = {}
|
151
|
+
parser = OptionParser.new
|
152
|
+
parser.on('-v', '--version') { options[:version] = true }
|
153
|
+
parser.on('-v', '--verbose') { options[:verbose] = true } # !!!!
|
154
|
+
argv = ["-v"]
|
155
|
+
parser.parse!(argv)
|
156
|
+
p options #=> {:verbose=>true}, not {:version=>true}
|
157
|
+
```
|
158
|
+
|
142
159
|
* `optparse.rb` adds `-h` and `--help` options automatically, and
|
143
160
|
terminates current process when `-h` or `--help` specified in command-line.
|
144
161
|
It is hard to remove these options.
|
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.3.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>
|
@@ -155,6 +155,23 @@ cmdopt = Benry::CmdOpt.new
|
|
155
155
|
cmdopt.add(:number, "-n <N>", "number", type: Integer, <strong>enum: [1, 2, 3]</strong>) # very intuitive
|
156
156
|
</pre>
|
157
157
|
<ul>
|
158
|
+
<li><code>optparse.rb</code> doesn't report error even when options are duplicated.
|
159
|
+
This specification makes debugging hard.
|
160
|
+
<p></p>
|
161
|
+
<code>benry/cmdopt.rb</code> reports error when options are duplicated.</li>
|
162
|
+
</ul>
|
163
|
+
<pre class="language-ruby">
|
164
|
+
require 'optparse'
|
165
|
+
|
166
|
+
options = {}
|
167
|
+
parser = OptionParser.new
|
168
|
+
parser.on(<strong>'-v'</strong>, '--version') { options[:version] = true }
|
169
|
+
parser.on(<strong>'-v'</strong>, '--verbose') { options[:verbose] = true } # !!!!
|
170
|
+
argv = ["-v"]
|
171
|
+
parser.parse!(argv)
|
172
|
+
p options #=> {:verbose=>true}, not {:version=>true}
|
173
|
+
</pre>
|
174
|
+
<ul>
|
158
175
|
<li><code>optparse.rb</code> adds <code>-h</code> and <code>--help</code> options automatically, and
|
159
176
|
terminates current process when <code>-h</code> or <code>--help</code> specified in command-line.
|
160
177
|
It is hard to remove these options.
|
data/doc/css/style.css
CHANGED
@@ -74,12 +74,12 @@ pre > strong {
|
|
74
74
|
font-weight: bold;
|
75
75
|
color: #900;
|
76
76
|
}
|
77
|
-
pre.language-terminal {
|
77
|
+
pre.language-terminal, pre.language-console {
|
78
78
|
background: #333;
|
79
79
|
color: #fff;
|
80
80
|
border-color: #000;
|
81
81
|
}
|
82
|
-
pre.language-terminal > strong {
|
82
|
+
pre.language-terminal > strong, pre.language-console > strong {
|
83
83
|
color: #f66;
|
84
84
|
}
|
85
85
|
code {
|
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.3.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.3.0 $'.split()[1]
|
27
27
|
|
28
28
|
|
29
29
|
def self.new()
|
@@ -138,16 +138,37 @@ module Benry::CmdOpt
|
|
138
138
|
#; [!yht0v] keeps command option definitions.
|
139
139
|
item = SchemaItem.new(key, optdef, desc, short, long, param, required,
|
140
140
|
type: type, rexp: rexp, enum: enum, range: range, value: value, detail: detail, hidden: hidden, important: important, tag: tag, &callback)
|
141
|
-
|
141
|
+
add_item(item)
|
142
142
|
item
|
143
143
|
end
|
144
144
|
|
145
145
|
def add_item(item)
|
146
|
+
#; [!qyjp9] raises SchemaError if invalid item added.
|
147
|
+
errmsg = _validate_item(item)
|
148
|
+
errmsg == nil or
|
149
|
+
raise SchemaError.new(errmsg)
|
146
150
|
#; [!a693h] adds option item into current schema.
|
147
151
|
@items << item
|
148
152
|
self
|
149
153
|
end
|
150
154
|
|
155
|
+
def _validate_item(item)
|
156
|
+
key = item.key; short = item.short; long = item.long
|
157
|
+
for x in @items
|
158
|
+
#; [!ewl20] returns error message if option key duplicated.
|
159
|
+
key != x.key or
|
160
|
+
return "#{key}: Option key duplicated."
|
161
|
+
#; [!xg56v] returns error message if short option duplicated.
|
162
|
+
short == nil || short != x.short or
|
163
|
+
return "-#{short}: Short option duplicated (key: #{key} and #{x.key})."
|
164
|
+
#; [!izezi] returns error message if long option duplicated.
|
165
|
+
long == nil || long != x.long or
|
166
|
+
return "--#{long}: Long option duplicated (key: #{key} and #{x.key})."
|
167
|
+
end
|
168
|
+
return nil
|
169
|
+
end
|
170
|
+
private :_validate_item
|
171
|
+
|
151
172
|
def option_help(width_or_format=nil, all: false)
|
152
173
|
#; [!0aq0i] can take integer as width.
|
153
174
|
#; [!pcsah] can take format string.
|
data/test/cmdopt_test.rb
CHANGED
@@ -258,6 +258,26 @@ Oktest.scope do
|
|
258
258
|
|
259
259
|
topic '#add_item()' do
|
260
260
|
|
261
|
+
spec "[!qyjp9] raises SchemaError if invalid item added." do
|
262
|
+
sc = Benry::CmdOpt::Schema.new
|
263
|
+
sc.add(:quiet, "-q, --quiet", "quiet mode")
|
264
|
+
#
|
265
|
+
item1 = Benry::CmdOpt::SchemaItem.new(:quiet, "-q", "quiet", "q", nil, nil, false)
|
266
|
+
pr = proc { sc.add_item(item1) }
|
267
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
268
|
+
"quiet: Option key duplicated.")
|
269
|
+
#
|
270
|
+
item2 = Benry::CmdOpt::SchemaItem.new(:quiet2, "-q", "quiet", "q", nil, nil, false)
|
271
|
+
pr = proc { sc.add_item(item2) }
|
272
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
273
|
+
"-q: Short option duplicated (key: quiet2 and quiet).")
|
274
|
+
#
|
275
|
+
item3 = Benry::CmdOpt::SchemaItem.new(:quiet3, "--quiet", "quiet", nil, "quiet", nil, false)
|
276
|
+
pr = proc { sc.add_item(item3) }
|
277
|
+
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
278
|
+
"--quiet: Long option duplicated (key: quiet3 and quiet).")
|
279
|
+
end
|
280
|
+
|
261
281
|
spec "[!a693h] adds option item into current schema." do
|
262
282
|
item = Benry::CmdOpt::SchemaItem.new(:quiet, "-q", "quiet", "q", nil, nil, false)
|
263
283
|
sc = Benry::CmdOpt::Schema.new
|
@@ -269,6 +289,35 @@ END
|
|
269
289
|
|
270
290
|
end
|
271
291
|
|
292
|
+
|
293
|
+
topic '#_validate_item()' do
|
294
|
+
|
295
|
+
before do
|
296
|
+
@schema = Benry::CmdOpt::Schema.new
|
297
|
+
@schema.add(:quiet, "-q, --quiet", "quiet mode")
|
298
|
+
end
|
299
|
+
|
300
|
+
spec "[!ewl20] returns error message if option key duplicated." do
|
301
|
+
item = Benry::CmdOpt::SchemaItem.new(:quiet, "-q", "quiet mode", "q", nil, nil, false)
|
302
|
+
ret = @schema.__send__(:_validate_item, item)
|
303
|
+
ok {ret} == "quiet: Option key duplicated."
|
304
|
+
end
|
305
|
+
|
306
|
+
spec "[!xg56v] returns error message if short option duplicated." do
|
307
|
+
item = Benry::CmdOpt::SchemaItem.new(:quiet2, "-q", "quiet mode", "q", nil, nil, false)
|
308
|
+
ret = @schema.__send__(:_validate_item, item)
|
309
|
+
ok {ret} == "-q: Short option duplicated (key: quiet2 and quiet)."
|
310
|
+
end
|
311
|
+
|
312
|
+
spec "[!izezi] returns error message if long option duplicated." do
|
313
|
+
item = Benry::CmdOpt::SchemaItem.new(:quiet3, "--quiet", "quiet mode", nil, "quiet", nil, false)
|
314
|
+
ret = @schema.__send__(:_validate_item, item)
|
315
|
+
ok {ret} == "--quiet: Long option duplicated (key: quiet3 and quiet)."
|
316
|
+
end
|
317
|
+
|
318
|
+
end
|
319
|
+
|
320
|
+
|
272
321
|
topic '#option_help()' do
|
273
322
|
|
274
323
|
before do
|
@@ -774,10 +823,10 @@ END
|
|
774
823
|
|
775
824
|
spec "[!melyd] raises SchemaError when enum is not an Array nor Set." do
|
776
825
|
sc = @schema
|
777
|
-
sc.add(:
|
778
|
-
sc.add(:
|
826
|
+
sc.add(:indent1, "-i <N>", "indent width", enum: ["2", "4", "8"])
|
827
|
+
sc.add(:indent2, "-j <N>", "indent width", enum: Set.new(["2", "4", "8"]))
|
779
828
|
pr = proc {
|
780
|
-
sc.add(:
|
829
|
+
sc.add(:indent3, "-k <N>", "indent width", enum: "2,4,8")
|
781
830
|
}
|
782
831
|
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
783
832
|
'"2,4,8": Array or set expected.')
|
@@ -847,7 +896,6 @@ END
|
|
847
896
|
@schema.add(:indent1, "-i <N>", "indent", type: Integer, range: range1)
|
848
897
|
@schema.add(:indent2, "-j <N>", "indent", type: Integer, range: range2)
|
849
898
|
}
|
850
|
-
pr.call
|
851
899
|
ok {pr}.NOT.raise?(Exception)
|
852
900
|
end
|
853
901
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: benry-cmdopt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kwatch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oktest
|