ejt_command_line 0.0.2 → 0.0.3
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 +9 -9
- data/lib/ejt_command_line/version.rb +1 -1
- data/lib/ejt_command_line.rb +31 -15
- data/spec/command_line_spec.rb +60 -15
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzFiZTljMTMyMDIwOWU5MTUxN2UwOTc2OTNkNGQzN2ZlMTE3ZTc1OQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
7
|
-
|
6
|
+
ZjU1MmE5YjczNmViMmUwNDEyN2ViNmFmMDNhOWIyNjllY2EwMTMzYw==
|
7
|
+
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NTA1YTcwYjQ4YTE5YWQzNTRkMzcxMGVmZDk1NTA5NDUyMmQ1YTJhZWU1YTIz
|
10
|
+
YjQ3ZGMwNmNmODY2OWYzNGYyNTUyMDllZjg2ZDIyZjNhNDZlNDAwNGM1OWZk
|
11
|
+
OTRhYzRmZjBjNWViNDRmNDllYWU1ZDhmYzZjYTY1NTgwMjFmZjY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MzIyM2RkNmQwNTk3N2QxNWEwZGY2ZGQ1YTY2MGY3MDlhZGQxMGU0NjcxYTlj
|
14
|
+
ZjQyZjEzMjc0MzZiNmRiZDk3NjdlNzJiYmZiZGFhYTVhNTdiYzZlNTM1MTZm
|
15
|
+
Y2NkOTk4Y2ZjYzc4ZmEzODJhZDQxNzNhOTdmMjBmYjA0OTVjNjE=
|
data/lib/ejt_command_line.rb
CHANGED
@@ -25,12 +25,14 @@ module CommandLine
|
|
25
25
|
|
26
26
|
#----------------------------------------------------------------
|
27
27
|
|
28
|
+
ValueType = Struct.new(:parser, :multi)
|
29
|
+
|
28
30
|
class Switch
|
29
|
-
attr_reader :flags, :
|
31
|
+
attr_reader :flags, :value_type
|
30
32
|
|
31
|
-
def initialize(flags,
|
33
|
+
def initialize(flags, value_type = nil)
|
32
34
|
@flags = flags
|
33
|
-
@
|
35
|
+
@value_type = value_type
|
34
36
|
end
|
35
37
|
|
36
38
|
def has_flag?(flag)
|
@@ -123,7 +125,15 @@ module CommandLine
|
|
123
125
|
raise ConfigureError, "duplicate value type '#{sym}'"
|
124
126
|
end
|
125
127
|
|
126
|
-
@value_types[sym] = parser
|
128
|
+
@value_types[sym] = ValueType.new(parser, false)
|
129
|
+
end
|
130
|
+
|
131
|
+
def multivalue_type(sym, &parser)
|
132
|
+
if @value_types.member?(sym)
|
133
|
+
raise ConfigureError, "duplicate value type '#{sym}'"
|
134
|
+
end
|
135
|
+
|
136
|
+
@value_types[sym] = ValueType.new(parser, true)
|
127
137
|
end
|
128
138
|
|
129
139
|
def simple_switch(sym, *flags)
|
@@ -131,7 +141,7 @@ module CommandLine
|
|
131
141
|
end
|
132
142
|
|
133
143
|
def value_switch(sym, value_sym, *flags)
|
134
|
-
@switches[sym] = Switch.new(flags,
|
144
|
+
@switches[sym] = Switch.new(flags, get_value_type(value_sym))
|
135
145
|
end
|
136
146
|
|
137
147
|
def global(&block)
|
@@ -174,15 +184,24 @@ module CommandLine
|
|
174
184
|
end
|
175
185
|
|
176
186
|
private
|
177
|
-
def parse_value(arg, s, args)
|
178
|
-
if s.
|
187
|
+
def parse_value(arg, s, args, old_value)
|
188
|
+
if s.value_type
|
179
189
|
if args.size == 0
|
180
190
|
raise ParseError, "no value specified for switch '#{arg}'"
|
181
191
|
end
|
182
192
|
|
183
193
|
value = args.shift
|
184
194
|
begin
|
185
|
-
s.parser.call(value)
|
195
|
+
v = s.value_type.parser.call(value)
|
196
|
+
if s.value_type.multi
|
197
|
+
if old_value.nil?
|
198
|
+
[v]
|
199
|
+
else
|
200
|
+
old_value << v
|
201
|
+
end
|
202
|
+
else
|
203
|
+
v
|
204
|
+
end
|
186
205
|
rescue => e
|
187
206
|
raise ParseError, "couldn't parse value '#{arg}=#{value}'\n#{e}"
|
188
207
|
end
|
@@ -203,7 +222,7 @@ module CommandLine
|
|
203
222
|
|
204
223
|
if arg =~ /^-/
|
205
224
|
sym, s = find_switch(valid_switches, arg)
|
206
|
-
opts[sym] = parse_value(arg, s, args)
|
225
|
+
opts[sym] = parse_value(arg, s, args, opts[sym])
|
207
226
|
|
208
227
|
else
|
209
228
|
cmd = arg.intern
|
@@ -242,12 +261,9 @@ module CommandLine
|
|
242
261
|
end
|
243
262
|
end
|
244
263
|
|
245
|
-
def
|
246
|
-
|
247
|
-
|
248
|
-
else
|
249
|
-
raise ConfigureError, "unknown value type '#{sym}'"
|
250
|
-
end
|
264
|
+
def get_value_type(sym)
|
265
|
+
raise ConfigureError, "unknown value type '#{sym}'" unless @value_types.member?(sym)
|
266
|
+
@value_types[sym]
|
251
267
|
end
|
252
268
|
|
253
269
|
def bracket_(release)
|
data/spec/command_line_spec.rb
CHANGED
@@ -15,7 +15,7 @@ describe "Parser" do
|
|
15
15
|
|
16
16
|
describe "creation" do
|
17
17
|
it "should take a config block" do
|
18
|
-
block_watcher =
|
18
|
+
block_watcher = double()
|
19
19
|
block_watcher.should_receive(:executed)
|
20
20
|
|
21
21
|
clh = Parser.new do
|
@@ -54,6 +54,32 @@ describe "Parser" do
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
+
describe "multi value types" do
|
58
|
+
it "should allow you to register new multi value types" do
|
59
|
+
@clh.configure do
|
60
|
+
multivalue_type :string do |str|
|
61
|
+
str
|
62
|
+
end
|
63
|
+
|
64
|
+
multivalue_type :int do |str|
|
65
|
+
str.to_i
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should fail it you try and define a duplicate value type" do
|
71
|
+
@clh.value_type :string do |str|
|
72
|
+
str
|
73
|
+
end
|
74
|
+
|
75
|
+
expect do
|
76
|
+
@clh.value_type :string do |str|
|
77
|
+
str
|
78
|
+
end
|
79
|
+
end.to raise_error(ConfigureError, /string/)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
57
83
|
describe "switches are defined separately from commands" do
|
58
84
|
it "should let you define binary switch" do
|
59
85
|
help_switch
|
@@ -123,18 +149,18 @@ describe "Parser" do
|
|
123
149
|
describe "parsing" do
|
124
150
|
describe "global command" do
|
125
151
|
it "should handle no switches" do
|
126
|
-
handler =
|
152
|
+
handler = double()
|
127
153
|
handler.should_receive(:global_command).with({}, [])
|
128
154
|
@clh.parse(handler)
|
129
155
|
end
|
130
156
|
|
131
157
|
it "should raise a ParseError if an unrecognised switch is used" do
|
132
|
-
handler =
|
158
|
+
handler = double()
|
133
159
|
expect {@clh.parse(handler, '--go-back-in-time')}.to raise_error(ParseError, /--go-back-in-time/)
|
134
160
|
end
|
135
161
|
|
136
162
|
it "should handle binary switches" do
|
137
|
-
handler =
|
163
|
+
handler = double()
|
138
164
|
handler.should_receive(:global_command).with({:help => true}, [])
|
139
165
|
|
140
166
|
@clh.configure do
|
@@ -148,7 +174,7 @@ describe "Parser" do
|
|
148
174
|
end
|
149
175
|
|
150
176
|
it "should handle multiple binary switches" do
|
151
|
-
handler =
|
177
|
+
handler = double()
|
152
178
|
handler.should_receive(:global_command).with({:help => true, :ro => true}, [])
|
153
179
|
|
154
180
|
@clh.configure do
|
@@ -164,7 +190,7 @@ describe "Parser" do
|
|
164
190
|
end
|
165
191
|
|
166
192
|
it "should handle valued switches" do
|
167
|
-
handler =
|
193
|
+
handler = double()
|
168
194
|
|
169
195
|
@clh.configure do
|
170
196
|
value_type :int do |str|
|
@@ -187,8 +213,27 @@ describe "Parser" do
|
|
187
213
|
@clh.parse(handler, 'one', '-c', '17', 'two')
|
188
214
|
end
|
189
215
|
|
216
|
+
it "should handle multivalued switches" do
|
217
|
+
handler = double()
|
218
|
+
|
219
|
+
@clh.configure do
|
220
|
+
multivalue_type :ints do |str|
|
221
|
+
str.to_i
|
222
|
+
end
|
223
|
+
|
224
|
+
value_switch :counts, :ints, '--count'
|
225
|
+
global do
|
226
|
+
switches :counts
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
handler.should_receive(:global_command).
|
231
|
+
with({:counts => [1, 3, 5]}, ['one', 'two'])
|
232
|
+
@clh.parse(handler, 'one', '--count', '1', 'two', '--count', '3', '--count', '5')
|
233
|
+
end
|
234
|
+
|
190
235
|
it "should raise an ArgumentError if no value is given for a valued switch" do
|
191
|
-
handler =
|
236
|
+
handler = double()
|
192
237
|
|
193
238
|
@clh.configure do
|
194
239
|
value_type :int do |str|
|
@@ -208,7 +253,7 @@ describe "Parser" do
|
|
208
253
|
end
|
209
254
|
|
210
255
|
it "should filter non-switches out" do
|
211
|
-
handler =
|
256
|
+
handler = double()
|
212
257
|
handler.should_receive(:global_command).
|
213
258
|
with({:help => true, :ro => true}, ['my_file', 'my_other_file'])
|
214
259
|
|
@@ -232,7 +277,7 @@ describe "Parser" do
|
|
232
277
|
end
|
233
278
|
end
|
234
279
|
|
235
|
-
handler =
|
280
|
+
handler = double()
|
236
281
|
handler.should_receive(:create).with({}, ['fred'])
|
237
282
|
@clh.parse(handler, 'create', 'fred')
|
238
283
|
end
|
@@ -265,13 +310,13 @@ describe "Parser" do
|
|
265
310
|
end
|
266
311
|
|
267
312
|
it "should allow you to define a sub command" do
|
268
|
-
handler =
|
313
|
+
handler = double()
|
269
314
|
handler.should_receive(:resize).with({:grow_to => 12345}, ['fred'])
|
270
315
|
@clh.parse(handler, 'resize', '--grow-to', '12345', 'fred')
|
271
316
|
end
|
272
317
|
|
273
318
|
it "should prevent you calling two sub commands on the same line" do
|
274
|
-
handler =
|
319
|
+
handler = double()
|
275
320
|
handler.should_receive(:resize).
|
276
321
|
with({:grow_to => 1234, :shrink_to => 2345}, ['shrink', 'fred'])
|
277
322
|
@clh.parse(handler, 'resize', '--grow-to', '1234', 'shrink', '--shrink-to', '2345', 'fred')
|
@@ -297,14 +342,14 @@ describe "Parser" do
|
|
297
342
|
end
|
298
343
|
|
299
344
|
it "should parse one exclusive switch" do
|
300
|
-
handler =
|
345
|
+
handler = double()
|
301
346
|
handler.should_receive(:resize).
|
302
347
|
with({:grow_to => 1234}, ['fred'])
|
303
348
|
@clh.parse(handler, 'resize', '--grow-to', '1234', 'fred')
|
304
349
|
end
|
305
350
|
|
306
351
|
it "should raise a ParseError if more than one switch from an exclusive set is defined" do
|
307
|
-
handler =
|
352
|
+
handler = double()
|
308
353
|
expect do
|
309
354
|
@clh.parse(handler, 'resize', '--grow-to', '1234', '--shrink-by', '2345', 'fred')
|
310
355
|
end.to raise_error(ParseError, /mutually exclusive/)
|
@@ -340,14 +385,14 @@ describe "Parser" do
|
|
340
385
|
end
|
341
386
|
|
342
387
|
it "should parse ok if mandatory switch is given" do
|
343
|
-
handler =
|
388
|
+
handler = double()
|
344
389
|
handler.should_receive(:resize).
|
345
390
|
with({:grow_to => 3}, ['fred'])
|
346
391
|
@clh.parse(handler, 'resize', '--grow-to', '3', 'fred')
|
347
392
|
end
|
348
393
|
|
349
394
|
it "should raise a ParseError if a mandatory switch is omitted" do
|
350
|
-
handler =
|
395
|
+
handler = double()
|
351
396
|
expect do
|
352
397
|
@clh.parse(handler, 'resize', 'fred')
|
353
398
|
end.to raise_error(ParseError, /grow_to/)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ejt_command_line
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Thornber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-03-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
91
|
version: '0'
|
92
92
|
requirements: []
|
93
93
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.
|
94
|
+
rubygems_version: 2.2.2
|
95
95
|
signing_key:
|
96
96
|
specification_version: 4
|
97
97
|
summary: Command line parser
|