simple_scripting 0.12.1 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/simple_scripting/argv.rb +2 -1
- data/lib/simple_scripting/version.rb +1 -1
- data/simple_scripting.gemspec +1 -1
- data/spec/simple_scripting/argv_spec.rb +63 -58
- data/spec/simple_scripting/configuration_spec.rb +4 -8
- data/spec/simple_scripting/tab_completion_spec.rb +0 -12
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a31e06185c711924565be44455735e02e55b31a98fc6782a101214cf9d37266
|
4
|
+
data.tar.gz: 2fa2139a689a85e1703604a3da2be186ac8596b9b17c0bc2af4c8c8244c8ad4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bbf4a7477108d85b62922592eedbc550f0896ca75265385c219c052636d0c45628cd93ca59603649f9ee9227cf00fdf2526732918efb0ab7d55fe62d4b158196
|
7
|
+
data.tar.gz: b754caa9b812a0d7a5882d5bc2c67b0e72a276da9385a2be70d423838f00da711a182fb1f9b67c4072176c73a9d4020acb3b2070fba54f83615edd830cc5aa5c
|
@@ -243,7 +243,8 @@ module SimpleScripting
|
|
243
243
|
end
|
244
244
|
|
245
245
|
parser_opts.on(*param_definition) do |value|
|
246
|
-
|
246
|
+
raise "Unexpected (nil; likely programmatic error) value for param definition #{param_definition}" if value.nil?
|
247
|
+
result[key] = value
|
247
248
|
end
|
248
249
|
end
|
249
250
|
|
data/simple_scripting.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.platform = Gem::Platform::RUBY
|
11
11
|
s.required_ruby_version = '>= 2.3.0'
|
12
12
|
s.authors = ["Saverio Miroddi"]
|
13
|
-
s.date = "
|
13
|
+
s.date = "2022-07-21"
|
14
14
|
s.email = ["saverio.pub2@gmail.com"]
|
15
15
|
s.homepage = "https://github.com/saveriomiroddi/simple_scripting"
|
16
16
|
s.summary = "Library for simplifying some typical scripting functionalities."
|
@@ -3,15 +3,12 @@ require_relative '../../lib/simple_scripting/argv.rb'
|
|
3
3
|
require 'stringio'
|
4
4
|
|
5
5
|
module SimpleScripting
|
6
|
-
|
7
6
|
describe Argv do
|
8
|
-
|
9
7
|
let(:output_buffer) do
|
10
8
|
StringIO.new
|
11
9
|
end
|
12
10
|
|
13
11
|
describe 'Basic functionality' do
|
14
|
-
|
15
12
|
let(:decoder_params) {[
|
16
13
|
['-a' ],
|
17
14
|
['-b', '"-b" description'],
|
@@ -26,7 +23,6 @@ module SimpleScripting
|
|
26
23
|
]}
|
27
24
|
|
28
25
|
context 'help' do
|
29
|
-
|
30
26
|
it 'should print help automatically by default' do
|
31
27
|
decoder_params.last[:arguments] = ['-h']
|
32
28
|
|
@@ -73,11 +69,10 @@ module SimpleScripting
|
|
73
69
|
raise_errors: true,
|
74
70
|
)
|
75
71
|
|
76
|
-
|
77
|
-
|
78
|
-
|
72
|
+
expect {
|
73
|
+
described_class.decode(*decoder_params)
|
74
|
+
}.to raise_error(Argv::ArgumentError, "Missing mandatory argument(s)")
|
79
75
|
end
|
80
|
-
|
81
76
|
end # context 'help'
|
82
77
|
|
83
78
|
it "should implement basic switches, with conversion, and arguments (all set)" do
|
@@ -111,8 +106,49 @@ module SimpleScripting
|
|
111
106
|
expect(actual_result).to eql(expected_result)
|
112
107
|
end
|
113
108
|
|
114
|
-
context "
|
109
|
+
context "booleans" do
|
110
|
+
VALID_BOOLS = {
|
111
|
+
'false' => false,
|
112
|
+
'true' => true,
|
113
|
+
}
|
115
114
|
|
115
|
+
INVALID_BOOLS = %w[falx FALSE TRUE]
|
116
|
+
|
117
|
+
VALID_BOOLS.each do |user_value, decoded_value|
|
118
|
+
it "should decode a #{decoded_value} value" do
|
119
|
+
decoder_params = [
|
120
|
+
["-b", "--mybool VAL", TrueClass],
|
121
|
+
output: output_buffer,
|
122
|
+
arguments: ['--mybool', 'false']
|
123
|
+
]
|
124
|
+
|
125
|
+
actual_result = described_class.decode(*decoder_params)
|
126
|
+
|
127
|
+
expected_result = {
|
128
|
+
mybool: false
|
129
|
+
}
|
130
|
+
|
131
|
+
expect(actual_result).to eql(expected_result)
|
132
|
+
end
|
133
|
+
end # context "booleans"
|
134
|
+
|
135
|
+
INVALID_BOOLS.each do |value|
|
136
|
+
it "should raise an error on invalid bool #{value.inspect}" do
|
137
|
+
decoder_params = [
|
138
|
+
["-b", "--mybool VAL", TrueClass],
|
139
|
+
output: output_buffer,
|
140
|
+
arguments: ['--mybool', value],
|
141
|
+
raise_errors: true,
|
142
|
+
]
|
143
|
+
|
144
|
+
expect {
|
145
|
+
described_class.decode(*decoder_params)
|
146
|
+
}.to raise_error(OptionParser::InvalidArgument)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
context "multiple optional arguments" do
|
116
152
|
let(:decoder_params) {[
|
117
153
|
'[optional1]',
|
118
154
|
'[optional2]',
|
@@ -143,11 +179,9 @@ module SimpleScripting
|
|
143
179
|
|
144
180
|
expect(actual_result).to eql(expected_result)
|
145
181
|
end
|
146
|
-
|
147
182
|
end
|
148
183
|
|
149
184
|
context "error handling" do
|
150
|
-
|
151
185
|
# All the other UTs use error raising, for convenience.
|
152
186
|
it "should print the error, with a previx, by default, instead of raising an error" do
|
153
187
|
decoder_params.last[:arguments] = []
|
@@ -167,9 +201,9 @@ module SimpleScripting
|
|
167
201
|
raise_errors: true,
|
168
202
|
)
|
169
203
|
|
170
|
-
|
171
|
-
|
172
|
-
|
204
|
+
expect {
|
205
|
+
described_class.decode(*decoder_params)
|
206
|
+
}.to raise_error(Argv::ArgumentError, "Missing mandatory argument(s)")
|
173
207
|
end
|
174
208
|
|
175
209
|
it "should raise an error when there are too many arguments" do
|
@@ -178,21 +212,16 @@ module SimpleScripting
|
|
178
212
|
raise_errors: true,
|
179
213
|
)
|
180
214
|
|
181
|
-
|
182
|
-
|
183
|
-
|
215
|
+
expect {
|
216
|
+
described_class.decode(*decoder_params)
|
217
|
+
}.to raise_error(Argv::ArgumentError, "Too many arguments")
|
184
218
|
end
|
185
|
-
|
186
219
|
end # context "error handling"
|
187
|
-
|
188
220
|
end # describe 'Basic functionality'
|
189
221
|
|
190
222
|
describe 'Varargs' do
|
191
|
-
|
192
223
|
describe '(mandatory)' do
|
193
|
-
|
194
224
|
context 'as only parameter' do
|
195
|
-
|
196
225
|
let(:decoder_params) {[
|
197
226
|
'*varargs',
|
198
227
|
output: output_buffer,
|
@@ -208,11 +237,9 @@ module SimpleScripting
|
|
208
237
|
|
209
238
|
expect(actual_result).to eql(expected_result)
|
210
239
|
end
|
211
|
-
|
212
240
|
end
|
213
241
|
|
214
242
|
context 'followed by varargs' do
|
215
|
-
|
216
243
|
let(:decoder_params) {[
|
217
244
|
'mandatory',
|
218
245
|
'*varargs',
|
@@ -230,11 +257,9 @@ module SimpleScripting
|
|
230
257
|
|
231
258
|
expect(actual_result).to eql(expected_result)
|
232
259
|
end
|
233
|
-
|
234
260
|
end
|
235
261
|
|
236
262
|
context "error handling" do
|
237
|
-
|
238
263
|
let(:decoder_params) {[
|
239
264
|
'*varargs',
|
240
265
|
output: output_buffer,
|
@@ -244,17 +269,14 @@ module SimpleScripting
|
|
244
269
|
it "should raise an error when they are not specified" do
|
245
270
|
decoder_params.last[:raise_errors] = true
|
246
271
|
|
247
|
-
|
248
|
-
|
249
|
-
|
272
|
+
expect {
|
273
|
+
described_class.decode(*decoder_params)
|
274
|
+
}.to raise_error(Argv::ArgumentError, "Missing mandatory argument(s)")
|
250
275
|
end
|
251
|
-
|
252
276
|
end # context "error handling"
|
253
|
-
|
254
277
|
end # describe '(mandatory)'
|
255
278
|
|
256
279
|
describe '(optional)' do
|
257
|
-
|
258
280
|
let(:decoder_params) {[
|
259
281
|
'[*varargs]',
|
260
282
|
output: output_buffer,
|
@@ -283,15 +305,11 @@ module SimpleScripting
|
|
283
305
|
|
284
306
|
expect(actual_result).to eql(expected_result)
|
285
307
|
end
|
286
|
-
|
287
308
|
end # describe '(optional)'
|
288
|
-
|
289
309
|
end # describe 'Varargs'
|
290
310
|
|
291
311
|
describe 'Commands' do
|
292
|
-
|
293
312
|
describe 'regular case' do
|
294
|
-
|
295
313
|
let(:decoder_params) {{
|
296
314
|
'command1' => [
|
297
315
|
'arg1',
|
@@ -314,16 +332,15 @@ module SimpleScripting
|
|
314
332
|
end
|
315
333
|
|
316
334
|
context "error handling" do
|
317
|
-
|
318
335
|
it "should raise an error on invalid command" do
|
319
336
|
decoder_params.merge!(
|
320
337
|
arguments: ['pizza'],
|
321
338
|
raise_errors: true,
|
322
339
|
)
|
323
340
|
|
324
|
-
|
325
|
-
|
326
|
-
|
341
|
+
expect {
|
342
|
+
described_class.decode(decoder_params)
|
343
|
+
}.to raise_error(an_instance_of(Argv::InvalidCommand).and having_attributes(
|
327
344
|
message: "Invalid command: pizza",
|
328
345
|
valid_commands: ["command1", "command2"],
|
329
346
|
))
|
@@ -335,18 +352,16 @@ module SimpleScripting
|
|
335
352
|
raise_errors: true,
|
336
353
|
)
|
337
354
|
|
338
|
-
|
339
|
-
|
340
|
-
|
355
|
+
expect {
|
356
|
+
described_class.decode(decoder_params)
|
357
|
+
}.to raise_error(an_instance_of(Argv::InvalidCommand).and having_attributes(
|
341
358
|
message: "Missing command!",
|
342
359
|
valid_commands: ["command1", "command2"],
|
343
360
|
))
|
344
361
|
end
|
345
|
-
|
346
362
|
end # context "error handling"
|
347
363
|
|
348
364
|
context "help" do
|
349
|
-
|
350
365
|
it 'should implement the commands help' do
|
351
366
|
decoder_params[:arguments] = ['-h']
|
352
367
|
|
@@ -377,7 +392,6 @@ module SimpleScripting
|
|
377
392
|
end
|
378
393
|
|
379
394
|
context 'auto_help: false' do
|
380
|
-
|
381
395
|
it 'should not interpret the --help argument, and not print the help' do
|
382
396
|
decoder_params.merge!(
|
383
397
|
arguments: ['-h'],
|
@@ -407,15 +421,11 @@ module SimpleScripting
|
|
407
421
|
|
408
422
|
expect(actual_result).to eql(expected_result)
|
409
423
|
end
|
410
|
-
|
411
424
|
end # context 'auto_help: false'
|
412
|
-
|
413
425
|
end # context 'help'
|
414
|
-
|
415
426
|
end # describe 'regular case'
|
416
427
|
|
417
428
|
describe 'Nested commands' do
|
418
|
-
|
419
429
|
let(:decoder_params) {{
|
420
430
|
'command1' => {
|
421
431
|
'nested1a' => [
|
@@ -481,13 +491,11 @@ module SimpleScripting
|
|
481
491
|
expect(output_buffer.string).to eql(expected_output)
|
482
492
|
end
|
483
493
|
end # describe 'Nested commands'
|
484
|
-
|
485
494
|
end # describe 'Commands'
|
486
495
|
|
487
496
|
# Special case.
|
488
497
|
#
|
489
498
|
describe 'No definitions given' do
|
490
|
-
|
491
499
|
let(:decoder_params) {{
|
492
500
|
output: output_buffer,
|
493
501
|
}}
|
@@ -496,13 +504,10 @@ module SimpleScripting
|
|
496
504
|
decoder_params[:arguments] = ['pizza']
|
497
505
|
decoder_params[:raise_errors] = true
|
498
506
|
|
499
|
-
|
500
|
-
|
501
|
-
|
507
|
+
expect {
|
508
|
+
described_class.decode(decoder_params)
|
509
|
+
}.to raise_error(Argv::ArgumentError, "Too many arguments")
|
502
510
|
end
|
503
|
-
|
504
511
|
end # describe 'No definitions given'
|
505
|
-
|
506
512
|
end # describe Argv
|
507
|
-
|
508
513
|
end # module SimpleScripting
|
@@ -4,7 +4,6 @@ require 'tempfile'
|
|
4
4
|
require 'tmpdir'
|
5
5
|
|
6
6
|
module SimpleScripting::ConfigurationSpecHelper
|
7
|
-
|
8
7
|
def with_tempfile(config_content)
|
9
8
|
tempfile = Tempfile.new('ss_config_test')
|
10
9
|
tempfile.write(config_content)
|
@@ -14,11 +13,9 @@ module SimpleScripting::ConfigurationSpecHelper
|
|
14
13
|
ensure
|
15
14
|
tempfile.unlink
|
16
15
|
end
|
17
|
-
|
18
16
|
end
|
19
17
|
|
20
18
|
describe SimpleScripting::Configuration do
|
21
|
-
|
22
19
|
include SimpleScripting::ConfigurationSpecHelper
|
23
20
|
|
24
21
|
let(:configuration_text) {"
|
@@ -56,9 +53,9 @@ g2_key=bang
|
|
56
53
|
|
57
54
|
it "should raise an error when required keys are missing" do
|
58
55
|
with_tempfile(configuration_text) do |config_file|
|
59
|
-
|
60
|
-
|
61
|
-
|
56
|
+
expect {
|
57
|
+
described_class.load(config_file: config_file, required: %w(abspath_key missing_key group1))
|
58
|
+
}.to raise_error(RuntimeError, "Missing required configuration key(s): missing_key, group1")
|
62
59
|
end
|
63
60
|
end
|
64
61
|
|
@@ -73,5 +70,4 @@ g2_key=bang
|
|
73
70
|
File.delete(temp_config_file)
|
74
71
|
end
|
75
72
|
end
|
76
|
-
|
77
|
-
end
|
73
|
+
end # describe SimpleScripting::Configuration
|
@@ -3,7 +3,6 @@
|
|
3
3
|
require_relative '../../lib/simple_scripting/tab_completion.rb'
|
4
4
|
|
5
5
|
describe SimpleScripting::TabCompletion do
|
6
|
-
|
7
6
|
include TabCompletionCustomRSpecMatchers
|
8
7
|
|
9
8
|
let(:output_buffer) {
|
@@ -45,9 +44,7 @@ describe SimpleScripting::TabCompletion do
|
|
45
44
|
subject { described_class.new(switches_definition, output: output_buffer) }
|
46
45
|
|
47
46
|
context "with a correct configuration" do
|
48
|
-
|
49
47
|
context "standard cases" do
|
50
|
-
|
51
48
|
# Note that the conversion of mandatory to optional argument is defined by most of the cases.
|
52
49
|
#
|
53
50
|
STANDARD_CASES = {
|
@@ -79,11 +76,9 @@ describe SimpleScripting::TabCompletion do
|
|
79
76
|
expect(symbolic_commandline_options).to complete_with(expected_entries)
|
80
77
|
end
|
81
78
|
end
|
82
|
-
|
83
79
|
end # context "standard cases"
|
84
80
|
|
85
81
|
context "suffix management" do
|
86
|
-
|
87
82
|
SUFFIX_CASES = {
|
88
83
|
"arg1<tab>v" => %w(arg1v1 arg1v2), # the execution target of the test suite doesn't
|
89
84
|
"arg1<tab>x" => %w(), # ignore the suffix; programmer-defined
|
@@ -97,11 +92,9 @@ describe SimpleScripting::TabCompletion do
|
|
97
92
|
expect(symbolic_commandline_options).to complete_with(expected_entries)
|
98
93
|
end
|
99
94
|
end
|
100
|
-
|
101
95
|
end # context "suffix management"
|
102
96
|
|
103
97
|
context "escaped cases" do
|
104
|
-
|
105
98
|
ESCAPED_CASES = {
|
106
99
|
"\ <tab>" => [" _argv1spc"],
|
107
100
|
'\-<tab>' => %w(), # this is the result of typing `command "\-<tab>`
|
@@ -111,7 +104,6 @@ describe SimpleScripting::TabCompletion do
|
|
111
104
|
ESCAPED_CASES.each do |symbolic_commandline_options, _|
|
112
105
|
it "should output the entries for #{symbolic_commandline_options.inspect}"
|
113
106
|
end
|
114
|
-
|
115
107
|
end # context "escaped cases"
|
116
108
|
|
117
109
|
it "should support multiple values for an option"
|
@@ -119,11 +111,9 @@ describe SimpleScripting::TabCompletion do
|
|
119
111
|
it "should keep parsing also when --help is passed" do
|
120
112
|
expect("--help a<tab>").to complete_with(%w(arg1v1 arg1v2))
|
121
113
|
end
|
122
|
-
|
123
114
|
end # context "with a correct configuration"
|
124
115
|
|
125
116
|
context "with an incorrect configuration" do
|
126
|
-
|
127
117
|
INCORRECT_CASES = [
|
128
118
|
"a b <tab>", # too many args
|
129
119
|
"-O<tab>", # no values for this option
|
@@ -134,7 +124,5 @@ describe SimpleScripting::TabCompletion do
|
|
134
124
|
expect(symbolic_commandline_options).to not_complete
|
135
125
|
end
|
136
126
|
end
|
137
|
-
|
138
127
|
end # context "with an incorrect configuration"
|
139
|
-
|
140
128
|
end # describe SimpleScripting::TabCompletion
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_scripting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Saverio Miroddi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parseconfig
|
@@ -99,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
99
|
- !ruby/object:Gem::Version
|
100
100
|
version: '0'
|
101
101
|
requirements: []
|
102
|
-
rubygems_version: 3.
|
102
|
+
rubygems_version: 3.1.6
|
103
103
|
signing_key:
|
104
104
|
specification_version: 4
|
105
105
|
summary: Library for simplifying some typical scripting functionalities.
|