ejt_command_line 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/ejt_command_line.rb +28 -29
- data/lib/ejt_command_line/version.rb +1 -1
- data/spec/command_line_spec.rb +17 -32
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZTM3ZWUyYmQzMjg2YzRlMjI0MmExY2VhZjUzNGQ5ZTRjNDVkNGQ0Yg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
Y2M5OGYyY2YwZjRkNDA2NTVhN2U0N2EyNjMxMjA2ODYzYmFhMDBiMQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MmFjNDQ5N2E2NThkMmNjODAxMGY3YmFjYjA1NGQyYmRhYWFkZTI2NTA5MmNk
|
10
|
+
YzIxYTRjOWEyNTZkYTdlMDc2YjVkNTE4YzBmNTk3ZTk2NmNlMDc2ZDhlY2Zi
|
11
|
+
MjM4NmQ2YmViZjllOTdmZWQwMDMxOWZmNzY0NTIwM2E3OWNkZDc=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZWExNWYzYjU2ZDM2NjFjYzVmN2VlYmFjNTM3YTc5MmFkOGIxNzJiZWFmMzVk
|
14
|
+
MWRhMDIwNGFmN2NiYjJjZTMyZTliZDBjMjFhNTVhNjI3OWQxNTYyYTZiZGFl
|
15
|
+
ZmZkODVmZjJiYzQ3MjZiZGJiYjhjNjhhZDFjNTI0ZjcwMmYyMzA=
|
data/lib/ejt_command_line.rb
CHANGED
@@ -25,14 +25,13 @@ module CommandLine
|
|
25
25
|
|
26
26
|
#----------------------------------------------------------------
|
27
27
|
|
28
|
-
ValueType = Struct.new(:parser, :multi)
|
29
|
-
|
30
28
|
class Switch
|
31
|
-
attr_reader :flags, :
|
29
|
+
attr_reader :flags, :parser, :multi
|
32
30
|
|
33
|
-
def initialize(flags,
|
31
|
+
def initialize(flags, parser = nil, multi = false)
|
34
32
|
@flags = flags
|
35
|
-
@
|
33
|
+
@parser = parser
|
34
|
+
@multi = multi
|
36
35
|
end
|
37
36
|
|
38
37
|
def has_flag?(flag)
|
@@ -125,15 +124,7 @@ module CommandLine
|
|
125
124
|
raise ConfigureError, "duplicate value type '#{sym}'"
|
126
125
|
end
|
127
126
|
|
128
|
-
@value_types[sym] =
|
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
|
+
@value_types[sym] = parser
|
137
128
|
end
|
138
129
|
|
139
130
|
def simple_switch(sym, *flags)
|
@@ -141,7 +132,11 @@ module CommandLine
|
|
141
132
|
end
|
142
133
|
|
143
134
|
def value_switch(sym, value_sym, *flags)
|
144
|
-
@switches[sym] = Switch.new(flags,
|
135
|
+
@switches[sym] = Switch.new(flags, get_value_parser(value_sym))
|
136
|
+
end
|
137
|
+
|
138
|
+
def multivalue_switch(sym, value_sym, *flags)
|
139
|
+
@switches[sym] = Switch.new(flags, get_value_parser(value_sym), true)
|
145
140
|
end
|
146
141
|
|
147
142
|
def global(&block)
|
@@ -184,23 +179,19 @@ module CommandLine
|
|
184
179
|
end
|
185
180
|
|
186
181
|
private
|
187
|
-
def parse_value(arg, s, args, old_value)
|
188
|
-
if s.
|
182
|
+
def parse_value(arg, s, args, old_value = nil)
|
183
|
+
if s.parser
|
189
184
|
if args.size == 0
|
190
185
|
raise ParseError, "no value specified for switch '#{arg}'"
|
191
186
|
end
|
192
187
|
|
193
188
|
value = args.shift
|
194
189
|
begin
|
195
|
-
v = s.
|
196
|
-
if
|
197
|
-
if old_value.nil?
|
198
|
-
[v]
|
199
|
-
else
|
200
|
-
old_value << v
|
201
|
-
end
|
202
|
-
else
|
190
|
+
v = s.parser.call(value)
|
191
|
+
if old_value.nil?
|
203
192
|
v
|
193
|
+
else
|
194
|
+
old_value << v
|
204
195
|
end
|
205
196
|
rescue => e
|
206
197
|
raise ParseError, "couldn't parse value '#{arg}=#{value}'\n#{e}"
|
@@ -222,7 +213,12 @@ module CommandLine
|
|
222
213
|
|
223
214
|
if arg =~ /^-/
|
224
215
|
sym, s = find_switch(valid_switches, arg)
|
225
|
-
|
216
|
+
|
217
|
+
if s.multi
|
218
|
+
opts[sym] = parse_value(arg, s, args, opts[sym] || [])
|
219
|
+
else
|
220
|
+
opts[sym] = parse_value(arg, s, args)
|
221
|
+
end
|
226
222
|
|
227
223
|
else
|
228
224
|
cmd = arg.intern
|
@@ -261,9 +257,12 @@ module CommandLine
|
|
261
257
|
end
|
262
258
|
end
|
263
259
|
|
264
|
-
def
|
265
|
-
|
266
|
-
|
260
|
+
def get_value_parser(sym)
|
261
|
+
if @value_types.member?(sym)
|
262
|
+
@value_types[sym]
|
263
|
+
else
|
264
|
+
raise ConfigureError, "unknown value type '#{sym}'"
|
265
|
+
end
|
267
266
|
end
|
268
267
|
|
269
268
|
def bracket_(release)
|
data/spec/command_line_spec.rb
CHANGED
@@ -54,32 +54,6 @@ 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
|
-
|
83
57
|
describe "switches are defined separately from commands" do
|
84
58
|
it "should let you define binary switch" do
|
85
59
|
help_switch
|
@@ -98,6 +72,16 @@ describe "Parser" do
|
|
98
72
|
value_switch :resize_to, :volume_size, '--resize-to'
|
99
73
|
end
|
100
74
|
end
|
75
|
+
|
76
|
+
it "should let you define an option that can occur multiple times" do
|
77
|
+
@clh.configure do
|
78
|
+
value_type :filter do |str|
|
79
|
+
str
|
80
|
+
end
|
81
|
+
|
82
|
+
multivalue_switch :test_class, :filter, '--test-class'
|
83
|
+
end
|
84
|
+
end
|
101
85
|
end
|
102
86
|
|
103
87
|
describe "global switches" do
|
@@ -213,23 +197,24 @@ describe "Parser" do
|
|
213
197
|
@clh.parse(handler, 'one', '-c', '17', 'two')
|
214
198
|
end
|
215
199
|
|
216
|
-
it "should handle
|
200
|
+
it "should handle multivalue switches" do
|
217
201
|
handler = double()
|
218
202
|
|
219
203
|
@clh.configure do
|
220
|
-
|
204
|
+
value_type :int do |str|
|
221
205
|
str.to_i
|
222
206
|
end
|
223
207
|
|
224
|
-
|
208
|
+
multivalue_switch :port, :int, '--port', '-p'
|
209
|
+
|
225
210
|
global do
|
226
|
-
switches :
|
211
|
+
switches :port
|
227
212
|
end
|
228
213
|
end
|
229
214
|
|
230
215
|
handler.should_receive(:global_command).
|
231
|
-
with({:
|
232
|
-
@clh.parse(handler,
|
216
|
+
with({:port => [8080, 8081]}, ['one', 'two'])
|
217
|
+
@clh.parse(handler, *%w[-p 8080 one -p 8081 two])
|
233
218
|
end
|
234
219
|
|
235
220
|
it "should raise an ArgumentError if no value is given for a valued switch" do
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Thornber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|