micro-optparse 1.2.0 → 1.2.1
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 +6 -5
- data/lib/micro-optparse/parser.rb +2 -2
- data/lib/micro-optparse/version.rb +1 -1
- data/spec/parser_spec.rb +77 -77
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5841fd47f0f0334f7e574fb4eeb4b8d3558459c
|
4
|
+
data.tar.gz: f4247e4c8222e4fbc36e8e1c711d72a5ba0501bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5be7e4d71a36166fe0bbbd4cb31e34799600317e991ed3e484a33265a35181ecad556bb44adc4880c421a5ffdc9acb98468a1887fa178f842a4fdc6da409061
|
7
|
+
data.tar.gz: c30b91a5c788683983105e8c958a86a1aa73ffbc8e1c0a95de5d0df6e9ccf16184fd9f6775234f0a2e808373bf0e188aa8fef567276bb1a996cacb99f451fe62
|
data/CHANGES.md
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
Version History
|
2
2
|
===============
|
3
3
|
|
4
|
+
Version 1.2.1 (2017-10-02)
|
5
|
+
--------------------------
|
6
|
+
|
7
|
+
* Adjusted parsing of default values to look for `Integer` rather `Fixnum`
|
8
|
+
to be compatible with Ruby 2.4.0.
|
9
|
+
|
4
10
|
Version 1.2.0 (2013-12-29)
|
5
11
|
--------------------------
|
6
12
|
|
data/README.md
CHANGED
@@ -48,12 +48,13 @@ What this piece of code does is the following:
|
|
48
48
|
* it creates a help message and help options, with the banner above the options
|
49
49
|
* it creates a version option, which displays the given text
|
50
50
|
* it creates a long accessor for each option, according to the symbol - for example `"--verbose"` for `:verbose`
|
51
|
-
* it
|
51
|
+
* it creates a short accessor, which is the first character of the long accessor (automatically resolves duplication)
|
52
|
+
* it converts under_score options in Ruby to hypenated-options for the CLI, e.g. `:plus_selection` becomes `--plus-selection`
|
52
53
|
* it checks if the class of the input and the default value match
|
53
|
-
* it creates a switch
|
54
|
-
* when
|
55
|
-
* when value_matches is given, it validates if the input value matches the regular expression
|
56
|
-
* when value_satisfies is given, it validates if the
|
54
|
+
* it creates a switch if no default value exist or the default value is `true` or `false`
|
55
|
+
* when `value_in_set` is given, it validates if the input value is in the given array
|
56
|
+
* when `value_matches` is given, it validates if the input value matches the regular expression
|
57
|
+
* when `value_satisfies` is given, it validates if the lambda or Proc evaluates to `true` when fed with the input value
|
57
58
|
* it stores all parsed arguments and default values in the options hash, i.e. to access the value of `:mutation` in your script, write `options[:mutation]`
|
58
59
|
|
59
60
|
The automatically generated help message looks like this:
|
@@ -46,7 +46,7 @@ class Parser
|
|
46
46
|
@used_short << short = o[:settings][:no_short] ? nil : o[:settings][:short] || short_from(o[:name])
|
47
47
|
@result[o[:name]] = o[:settings][:default] || false unless o[:settings][:optional] # set default
|
48
48
|
name = o[:name].to_s.gsub("_", "-")
|
49
|
-
klass = o[:settings][:default].
|
49
|
+
klass = o[:settings][:default].is_a?(Integer) ? Integer : o[:settings][:default].class
|
50
50
|
|
51
51
|
args = [o[:description]]
|
52
52
|
args << "-" + short if short
|
@@ -70,7 +70,7 @@ class Parser
|
|
70
70
|
rescue OptionParser::ParseError => e
|
71
71
|
puts e.message ; exit(1)
|
72
72
|
end
|
73
|
-
|
73
|
+
|
74
74
|
validate(@result) if self.respond_to?("validate")
|
75
75
|
@result
|
76
76
|
end
|
data/spec/parser_spec.rb
CHANGED
@@ -11,21 +11,21 @@ describe Parser do
|
|
11
11
|
p.option :chance, "set mutation chance", :default => 0.8, :value_satisfies => lambda {|x| x >= 0.0 && x <= 1.0}
|
12
12
|
end
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
describe "parsing of default values" do
|
16
16
|
it "should assume false as default value if no default value was given" do
|
17
17
|
result = @evolutionary_algorithm_parser.process!([])
|
18
|
-
result[:verbose].
|
18
|
+
expect(result[:verbose]).to eql false
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
it "should return default values if called without arguments" do
|
22
22
|
result = @evolutionary_algorithm_parser.process!([])
|
23
|
-
result[:severity].
|
24
|
-
result[:verbose].
|
25
|
-
result[:mutation].
|
26
|
-
result[:plus_selection].
|
27
|
-
result[:selection].
|
28
|
-
result[:chance].
|
23
|
+
expect(result[:severity]).to eql 4
|
24
|
+
expect(result[:verbose]).to eql false
|
25
|
+
expect(result[:mutation]).to eql "MightyMutation"
|
26
|
+
expect(result[:plus_selection]).to eql true
|
27
|
+
expect(result[:selection]).to eql "BestSelection"
|
28
|
+
expect(result[:chance]).to eql 0.8
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should not return a default value if the argument is declared optional" do
|
@@ -33,8 +33,8 @@ describe Parser do
|
|
33
33
|
p.option :optarg, "optional argument", :optional => true
|
34
34
|
end
|
35
35
|
result = parser.process!()
|
36
|
-
result.has_key?(:optarg).
|
37
|
-
result[:optarg].
|
36
|
+
expect(result.has_key?(:optarg)).to eql false
|
37
|
+
expect(result[:optarg]).to eql nil
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -43,59 +43,59 @@ describe Parser do
|
|
43
43
|
args = ["--severity", "5", "--verbose", "--mutation", "DumbMutation",
|
44
44
|
"--no-plus-selection", "--selection", "WorstSelection", "--chance", "0.1"]
|
45
45
|
result = @evolutionary_algorithm_parser.process!(args)
|
46
|
-
result[:severity].
|
47
|
-
result[:verbose].
|
48
|
-
result[:mutation].
|
49
|
-
result[:plus_selection].
|
50
|
-
result[:selection].
|
51
|
-
result[:chance].
|
52
|
-
end
|
53
|
-
|
46
|
+
expect(result[:severity]).to eql 5
|
47
|
+
expect(result[:verbose]).to eql true
|
48
|
+
expect(result[:mutation]).to eql "DumbMutation"
|
49
|
+
expect(result[:plus_selection]).to eql false
|
50
|
+
expect(result[:selection]).to eql "WorstSelection"
|
51
|
+
expect(result[:chance]).to eql 0.1
|
52
|
+
end
|
53
|
+
|
54
54
|
it "should display overwritten values accordingly when the 'long=value' form was used" do
|
55
55
|
args = ["--severity=5", "--mutation=DumbMutation", "--selection=WorstSelection", "--chance=0.1"]
|
56
56
|
result = @evolutionary_algorithm_parser.process!(args)
|
57
|
-
result[:severity].
|
58
|
-
result[:mutation].
|
59
|
-
result[:selection].
|
60
|
-
result[:chance].
|
57
|
+
expect(result[:severity]).to eql 5
|
58
|
+
expect(result[:mutation]).to eql "DumbMutation"
|
59
|
+
expect(result[:selection]).to eql "WorstSelection"
|
60
|
+
expect(result[:chance]).to eql 0.1
|
61
61
|
end
|
62
62
|
|
63
63
|
it "should display overwritten values accordingly when short option names were used" do
|
64
64
|
# there is no short form to set switches to false
|
65
65
|
args = ["-s", "5", "-v", "-m", "DumbMutation", "--no-plus-selection", "-l", "WorstSelection", "-c", "0.1"]
|
66
66
|
result = @evolutionary_algorithm_parser.process!(args)
|
67
|
-
result[:severity].
|
68
|
-
result[:verbose].
|
69
|
-
result[:mutation].
|
70
|
-
result[:plus_selection].
|
71
|
-
result[:selection].
|
72
|
-
result[:chance].
|
67
|
+
expect(result[:severity]).to eql 5
|
68
|
+
expect(result[:verbose]).to eql true
|
69
|
+
expect(result[:mutation]).to eql "DumbMutation"
|
70
|
+
expect(result[:plus_selection]).to eql false
|
71
|
+
expect(result[:selection]).to eql "WorstSelection"
|
72
|
+
expect(result[:chance]).to eql 0.1
|
73
73
|
end
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
describe "parsing of several arrays using the same parser" do
|
77
77
|
it "should not manipulate old results" do
|
78
78
|
result1 = @evolutionary_algorithm_parser.process!(["--severity=5"])
|
79
79
|
result2 = @evolutionary_algorithm_parser.process!(["--severity=6"])
|
80
80
|
result3 = @evolutionary_algorithm_parser.process!([])
|
81
81
|
|
82
|
-
result1[:severity].
|
83
|
-
result2[:severity].
|
84
|
-
result3[:severity].
|
82
|
+
expect(result1[:severity]).to eql 5
|
83
|
+
expect(result2[:severity]).to eql 6
|
84
|
+
expect(result3[:severity]).to eql 4
|
85
85
|
end
|
86
86
|
end
|
87
|
-
|
87
|
+
|
88
88
|
describe "empty parser" do
|
89
89
|
it "should be allowed to create a parser with an empty block" do
|
90
90
|
parser = Parser.new { }
|
91
|
-
parser.
|
92
|
-
parser.class.
|
91
|
+
expect(parser).not_to be_nil
|
92
|
+
expect(parser.class).to eql Parser
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
it "should be allowed to create a parser without a block" do
|
96
96
|
parser = Parser.new
|
97
|
-
parser.
|
98
|
-
parser.class.
|
97
|
+
expect(parser).not_to be_nil
|
98
|
+
expect(parser.class).to eql Parser
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
@@ -106,7 +106,7 @@ describe Parser do
|
|
106
106
|
end
|
107
107
|
|
108
108
|
input = ['--listarg', 'foo,bar,baz']
|
109
|
-
parser.process!(input)[:listarg].
|
109
|
+
expect(parser.process!(input)[:listarg]).to eql ['foo', 'bar', 'baz']
|
110
110
|
end
|
111
111
|
|
112
112
|
it "should allow multiple argument lists" do
|
@@ -117,8 +117,8 @@ describe Parser do
|
|
117
117
|
|
118
118
|
input = ['-f', 'foo,bar,baz', '-s', 'blah,blah,blah']
|
119
119
|
result = parser.process!(input)
|
120
|
-
result[:first_listarg].
|
121
|
-
result[:second_listarg].
|
120
|
+
expect(result[:first_listarg]).to eql ['foo', 'bar', 'baz']
|
121
|
+
expect(result[:second_listarg]).to eql ['blah', 'blah', 'blah']
|
122
122
|
end
|
123
123
|
end
|
124
124
|
|
@@ -130,7 +130,7 @@ describe Parser do
|
|
130
130
|
end
|
131
131
|
|
132
132
|
result = parser.process!([])
|
133
|
-
result.length.
|
133
|
+
expect(result.length).to eql 0 # all optional
|
134
134
|
end
|
135
135
|
|
136
136
|
it "should allow to overwrite default settings" do
|
@@ -140,17 +140,17 @@ describe Parser do
|
|
140
140
|
end
|
141
141
|
|
142
142
|
result = parser.process!([])
|
143
|
-
result[:foo].
|
144
|
-
result[:bar].
|
143
|
+
expect(result[:foo]).to eql "Foo"
|
144
|
+
expect(result[:bar]).to eql "Bar"
|
145
145
|
end
|
146
146
|
end
|
147
|
-
|
147
|
+
|
148
148
|
describe "help message" do
|
149
149
|
it "should show help message when called with --help or -h" do
|
150
150
|
results = [`ruby spec/programs/eating.rb -h`, `ruby spec/programs/eating.rb --help`]
|
151
151
|
results.each do |result|
|
152
|
-
result.
|
153
|
-
result.
|
152
|
+
expect(result).to include("--help")
|
153
|
+
expect(result).to include("Show this message")
|
154
154
|
end
|
155
155
|
end
|
156
156
|
end
|
@@ -158,12 +158,12 @@ describe Parser do
|
|
158
158
|
describe "banner message" do
|
159
159
|
it "should include the banner info in the help message" do
|
160
160
|
result = `ruby spec/programs/eating.rb --help`
|
161
|
-
result.
|
161
|
+
expect(result).to include("This is a banner")
|
162
162
|
end
|
163
163
|
|
164
164
|
it "should include the default banner info if no banner message was set" do
|
165
165
|
result = `ruby spec/programs/empty.rb --help`
|
166
|
-
result.
|
166
|
+
expect(result).to include("Usage: empty [options]")
|
167
167
|
end
|
168
168
|
end
|
169
169
|
|
@@ -172,13 +172,13 @@ describe Parser do
|
|
172
172
|
# here -V is used for version, as -v is already taken for the verbose switch
|
173
173
|
results = [`ruby spec/programs/eating.rb -V`, `ruby spec/programs/eating.rb --version`]
|
174
174
|
results.each do |result|
|
175
|
-
result.strip.
|
175
|
+
expect(result.strip).to eql "EatingScript 1.0 (c) Florian Pilz 2011"
|
176
176
|
end
|
177
177
|
end
|
178
178
|
|
179
179
|
it "should display the version when called with -v" do
|
180
180
|
result = `ruby spec/programs/version.rb -v`
|
181
|
-
result.strip.
|
181
|
+
expect(result.strip).to eql "VersionScript 0.0 (c) Florian Pilz 2011"
|
182
182
|
end
|
183
183
|
|
184
184
|
it "should display a warning when --version or -v was called but no version was set" do
|
@@ -187,7 +187,7 @@ describe Parser do
|
|
187
187
|
`ruby spec/programs/empty.rb -v 2>&1`
|
188
188
|
]
|
189
189
|
results.each do |result|
|
190
|
-
result.strip.
|
190
|
+
expect(result.strip).to eql "empty: version unknown"
|
191
191
|
end
|
192
192
|
end
|
193
193
|
end
|
@@ -195,82 +195,82 @@ describe Parser do
|
|
195
195
|
describe "warnings from optparse" do
|
196
196
|
it "should display a warning if an argument was invalid" do
|
197
197
|
result = `ruby spec/programs/eating.rb --free-beer`
|
198
|
-
result.strip.
|
198
|
+
expect(result.strip).to eql "invalid option: --free-beer"
|
199
199
|
end
|
200
200
|
|
201
201
|
it "should display a warning if another argument is needed" do
|
202
202
|
result = `ruby spec/programs/eating.rb --eat-cake`
|
203
|
-
result.strip.
|
203
|
+
expect(result.strip).to eql "missing argument: --eat-cake"
|
204
204
|
end
|
205
205
|
|
206
206
|
it "should display a warning if an argument of the wrong type was given" do
|
207
207
|
result = `ruby spec/programs/eating.rb --eat-marshmellows OMFG!!!`
|
208
|
-
result.strip.
|
208
|
+
expect(result.strip).to eql "invalid argument: --eat-marshmellows OMFG!!!"
|
209
209
|
end
|
210
210
|
|
211
211
|
it "should display a warning if autocompletion of an argument was ambiguous" do
|
212
212
|
result = `ruby spec/programs/eating.rb --eat yummy!`
|
213
|
-
result.strip.
|
213
|
+
expect(result.strip).to eql "ambiguous option: --eat"
|
214
214
|
end
|
215
215
|
end
|
216
216
|
|
217
217
|
describe "warnings if validation failed" do
|
218
218
|
it "should display a warning if validation value_in_set failed" do
|
219
219
|
result = `ruby spec/programs/eating.rb --eat-bagel AshBagel`
|
220
|
-
result.strip.
|
220
|
+
expect(result.strip).to match(/Parameter for --eat-bagel must be in \[SalmonBagel,\s?ParmesanBagel\]/)
|
221
221
|
end
|
222
222
|
|
223
223
|
it "should display a warning if validation value_matches failed" do
|
224
224
|
result = `ruby spec/programs/eating.rb --eat-cake Chocolate`
|
225
|
-
result.strip.
|
225
|
+
expect(result.strip).to eql "Parameter for --eat-cake must match /Cake/"
|
226
226
|
end
|
227
227
|
|
228
228
|
it "should display a warning if validation value_satisfies failed" do
|
229
229
|
result = `ruby spec/programs/eating.rb --eat-cake 12Cakes`
|
230
|
-
result.strip.
|
230
|
+
expect(result.strip).to eql "Parameter for --eat-cake must satisfy given conditions (see description)"
|
231
231
|
end
|
232
232
|
|
233
233
|
it "should validate all validations if several are given for an option" do
|
234
234
|
result = `ruby spec/programs/eating.rb --eat-cake VanillaBrownie`
|
235
|
-
result.strip.
|
235
|
+
expect(result.strip).to eql "Parameter for --eat-cake must match /Cake/"
|
236
236
|
|
237
237
|
result = `ruby spec/programs/eating.rb --eat-cake 2VanillaCakes`
|
238
|
-
result.strip.
|
238
|
+
expect(result.strip).to eql "Parameter for --eat-cake must satisfy given conditions (see description)"
|
239
239
|
end
|
240
240
|
end
|
241
241
|
|
242
242
|
describe "automatic assignment of default accessors" do
|
243
243
|
it "should assign a different character for the short accessor if the first / second / ... is already taken" do
|
244
244
|
result = `ruby spec/programs/eating.rb --help`
|
245
|
-
result.
|
246
|
-
result.
|
247
|
-
result.
|
248
|
-
result.
|
249
|
-
result.
|
250
|
-
result.
|
245
|
+
expect(result).to include("--eat-cake")
|
246
|
+
expect(result).to include("-a, --eat-salad")
|
247
|
+
expect(result).to include("-t, --eat-bagel")
|
248
|
+
expect(result).to include("-n, --[no-]eat-nothing")
|
249
|
+
expect(result).to include("-m, --eat-marshmellow")
|
250
|
+
expect(result).to include("-e, --eat-me")
|
251
251
|
end
|
252
252
|
end
|
253
253
|
|
254
254
|
describe "assigns short for every param" do
|
255
255
|
it "should use every short only once" do
|
256
256
|
result = `ruby spec/programs/short.rb --help`
|
257
|
-
result.scan(/\s-a/).length.
|
258
|
-
result.scan(/\s-b/).length.
|
259
|
-
result.scan(/\s-c/).length.
|
257
|
+
expect(result.scan(/\s-a/).length).to eql 1
|
258
|
+
expect(result.scan(/\s-b/).length).to eql 1
|
259
|
+
expect(result.scan(/\s-c/).length).to eql 1
|
260
260
|
end
|
261
261
|
|
262
262
|
it "should use first char as short if all have been used" do
|
263
263
|
result = `ruby spec/programs/short.rb --help`
|
264
|
-
result.
|
265
|
-
result.
|
266
|
-
result.
|
264
|
+
expect(result).to include("-a, --acb")
|
265
|
+
expect(result).to include("-b, --bac")
|
266
|
+
expect(result).to include("-c, --cba")
|
267
267
|
end
|
268
268
|
|
269
269
|
it "should be possible to prevent creation of short arguments" do
|
270
270
|
result = `ruby spec/programs/noshort.rb --help`
|
271
|
-
result.
|
272
|
-
result.
|
273
|
-
result.
|
271
|
+
expect(result).not_to include("-f, --foo")
|
272
|
+
expect(result).to include("--foo")
|
273
|
+
expect(result).to include("-b, --bar")
|
274
274
|
end
|
275
275
|
end
|
276
276
|
end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: micro-optparse
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Pilz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
description: This is an lightweight option parser, which is less than 80 lines short.
|
@@ -34,7 +34,7 @@ executables: []
|
|
34
34
|
extensions: []
|
35
35
|
extra_rdoc_files: []
|
36
36
|
files:
|
37
|
-
- .gitignore
|
37
|
+
- ".gitignore"
|
38
38
|
- CHANGES.md
|
39
39
|
- Gemfile
|
40
40
|
- MIT-LICENSE
|
@@ -60,17 +60,17 @@ require_paths:
|
|
60
60
|
- lib
|
61
61
|
required_ruby_version: !ruby/object:Gem::Requirement
|
62
62
|
requirements:
|
63
|
-
- -
|
63
|
+
- - ">="
|
64
64
|
- !ruby/object:Gem::Version
|
65
65
|
version: '0'
|
66
66
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
67
|
requirements:
|
68
|
-
- -
|
68
|
+
- - ">="
|
69
69
|
- !ruby/object:Gem::Version
|
70
70
|
version: '0'
|
71
71
|
requirements: []
|
72
72
|
rubyforge_project:
|
73
|
-
rubygems_version: 2.
|
73
|
+
rubygems_version: 2.6.13
|
74
74
|
signing_key:
|
75
75
|
specification_version: 4
|
76
76
|
summary: An lightweight option parser, which is 80 lines short.
|