benry-cmdopt 2.1.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 +13 -0
- 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 +31 -3
- data/test/cmdopt_test.rb +65 -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,6 +2,19 @@ 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
|
+
|
11
|
+
Release 2.2.0 (2023-10-28)
|
12
|
+
--------------------------
|
13
|
+
|
14
|
+
* [enhance] Define `Schema#add_item()` which adds option item into the schema. This is for Benry-CmdApp framework.
|
15
|
+
* [change] (EXPERIMENTAL) Make `desc`, `detail`, `hidden`, `important`, `tag` attrubtes of `SchemaItem` object to be writable. This is for Benry-CmdApp framework.
|
16
|
+
|
17
|
+
|
5
18
|
Release 2.1.0 (2023-10-15)
|
6
19
|
--------------------------
|
7
20
|
|
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,10 +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
|
+
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)
|
150
|
+
#; [!a693h] adds option item into current schema.
|
151
|
+
@items << item
|
152
|
+
self
|
153
|
+
end
|
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
|
+
|
145
172
|
def option_help(width_or_format=nil, all: false)
|
146
173
|
#; [!0aq0i] can take integer as width.
|
147
174
|
#; [!pcsah] can take format string.
|
@@ -320,6 +347,7 @@ module Benry::CmdOpt
|
|
320
347
|
end
|
321
348
|
|
322
349
|
attr_reader :key, :optdef, :desc, :short, :long, :param, :type, :rexp, :enum, :range, :detail, :value, :tag, :callback
|
350
|
+
attr_writer :desc, :detail, :hidden, :important, :tag # !!experimental!!
|
323
351
|
alias pattern rexp # for backward compatibility
|
324
352
|
alias help desc # for backward compatibility
|
325
353
|
|
data/test/cmdopt_test.rb
CHANGED
@@ -256,6 +256,68 @@ Oktest.scope do
|
|
256
256
|
end
|
257
257
|
|
258
258
|
|
259
|
+
topic '#add_item()' do
|
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
|
+
|
281
|
+
spec "[!a693h] adds option item into current schema." do
|
282
|
+
item = Benry::CmdOpt::SchemaItem.new(:quiet, "-q", "quiet", "q", nil, nil, false)
|
283
|
+
sc = Benry::CmdOpt::Schema.new
|
284
|
+
sc.add_item(item)
|
285
|
+
ok {sc.to_s} == <<"END"
|
286
|
+
-q : quiet
|
287
|
+
END
|
288
|
+
end
|
289
|
+
|
290
|
+
end
|
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
|
+
|
259
321
|
topic '#option_help()' do
|
260
322
|
|
261
323
|
before do
|
@@ -761,10 +823,10 @@ END
|
|
761
823
|
|
762
824
|
spec "[!melyd] raises SchemaError when enum is not an Array nor Set." do
|
763
825
|
sc = @schema
|
764
|
-
sc.add(:
|
765
|
-
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"]))
|
766
828
|
pr = proc {
|
767
|
-
sc.add(:
|
829
|
+
sc.add(:indent3, "-k <N>", "indent width", enum: "2,4,8")
|
768
830
|
}
|
769
831
|
ok {pr}.raise?(Benry::CmdOpt::SchemaError,
|
770
832
|
'"2,4,8": Array or set expected.')
|
@@ -834,7 +896,6 @@ END
|
|
834
896
|
@schema.add(:indent1, "-i <N>", "indent", type: Integer, range: range1)
|
835
897
|
@schema.add(:indent2, "-j <N>", "indent", type: Integer, range: range2)
|
836
898
|
}
|
837
|
-
pr.call
|
838
899
|
ok {pr}.NOT.raise?(Exception)
|
839
900
|
end
|
840
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
|