libclimate-ruby 0.2.4 → 0.3.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/lib/libclimate/climate.rb +102 -12
- data/lib/libclimate/version.rb +1 -1
- data/test/unit/tc_with_blocks.rb +53 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ddeb7a8f3fbcbe387ff00dc9c0413a2921a1573
|
4
|
+
data.tar.gz: 205642b3c90c1c34685163a87534b496e10b1e97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fa38dafe06b342668e826492f9219904833f1e0a91feb11754f8731187bf42106fcad467ec08c7b8d360fb6ddeb4c9cf68c1bd63ba69aa54a206126a79c1a79
|
7
|
+
data.tar.gz: 3c8de16fba64582e4d4fc9ec9bf121c924de7b3a736972c413f2e28b86c47446bd9154daed06658e5d303e6c3f4c1946e66b57aebad730fac5e3809766d130cf
|
data/lib/libclimate/climate.rb
CHANGED
@@ -54,6 +54,75 @@ if !defined? Colcon
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
+
#:stopdoc:
|
58
|
+
|
59
|
+
# We monkey-patch CLASP module's Flag and Option generator methods by
|
60
|
+
# added in a 'action' attribute (but only if it does not exist)
|
61
|
+
# and attaching the given block
|
62
|
+
|
63
|
+
class << CLASP
|
64
|
+
|
65
|
+
alias_method :Flag_old, :Flag
|
66
|
+
alias_method :Option_old, :Option
|
67
|
+
|
68
|
+
def Flag(name, options={}, &blk)
|
69
|
+
|
70
|
+
f = self.Flag_old(name, options)
|
71
|
+
|
72
|
+
# anticipate this functionality being added to CLASP
|
73
|
+
return f if f.respond_to? :action
|
74
|
+
|
75
|
+
class << f
|
76
|
+
|
77
|
+
attr_accessor :action
|
78
|
+
end
|
79
|
+
|
80
|
+
if blk
|
81
|
+
|
82
|
+
case blk.arity
|
83
|
+
when 0, 1, 2
|
84
|
+
else
|
85
|
+
|
86
|
+
warn "wrong arity for flag"
|
87
|
+
end
|
88
|
+
|
89
|
+
f.action = blk
|
90
|
+
end
|
91
|
+
|
92
|
+
f
|
93
|
+
end
|
94
|
+
|
95
|
+
def Option(name, options={}, &blk)
|
96
|
+
|
97
|
+
o = self.Option_old(name, options)
|
98
|
+
|
99
|
+
# anticipate this functionality being added to CLASP
|
100
|
+
return o if o.respond_to? :action
|
101
|
+
|
102
|
+
class << o
|
103
|
+
|
104
|
+
attr_accessor :action
|
105
|
+
end
|
106
|
+
|
107
|
+
if blk
|
108
|
+
|
109
|
+
case blk.arity
|
110
|
+
when 0, 1, 2
|
111
|
+
else
|
112
|
+
|
113
|
+
warn "wrong arity for option"
|
114
|
+
end
|
115
|
+
|
116
|
+
o.action = blk
|
117
|
+
end
|
118
|
+
|
119
|
+
o
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
#:startdoc:
|
124
|
+
|
125
|
+
|
57
126
|
module LibCLImate
|
58
127
|
|
59
128
|
# Class used to gather together the CLI specification, and execute it
|
@@ -189,15 +258,26 @@ class Climate
|
|
189
258
|
|
190
259
|
selector = :unhandled
|
191
260
|
|
192
|
-
|
261
|
+
# see if it has a :action attribute (which will have been
|
262
|
+
# monkey-patched to CLASP.Flag()
|
263
|
+
|
264
|
+
if al.respond_to?(:action) && !al.action.nil?
|
265
|
+
|
266
|
+
al.action.call(f, al)
|
267
|
+
|
268
|
+
selector = :handled
|
269
|
+
else
|
193
270
|
|
194
|
-
|
195
|
-
when ::Hash
|
196
|
-
if ex.has_key? :handle
|
271
|
+
ex = al.extras
|
197
272
|
|
198
|
-
|
273
|
+
case ex
|
274
|
+
when ::Hash
|
275
|
+
if ex.has_key? :handle
|
199
276
|
|
200
|
-
|
277
|
+
ex[:handle].call(f, al)
|
278
|
+
|
279
|
+
selector = :handled
|
280
|
+
end
|
201
281
|
end
|
202
282
|
end
|
203
283
|
|
@@ -229,15 +309,25 @@ class Climate
|
|
229
309
|
|
230
310
|
selector = :unhandled
|
231
311
|
|
232
|
-
|
312
|
+
# see if it has a :action attribute (which will have been
|
313
|
+
# monkey-patched to CLASP.Option()
|
314
|
+
|
315
|
+
if al.respond_to?(:action) && !al.action.nil?
|
316
|
+
|
317
|
+
al.action.call(o, al)
|
318
|
+
|
319
|
+
selector = :handled
|
320
|
+
else
|
321
|
+
ex = al.extras
|
233
322
|
|
234
|
-
|
235
|
-
|
236
|
-
|
323
|
+
case ex
|
324
|
+
when ::Hash
|
325
|
+
if ex.has_key? :handle
|
237
326
|
|
238
|
-
|
327
|
+
ex[:handle].call(o, al)
|
239
328
|
|
240
|
-
|
329
|
+
selector = :handled
|
330
|
+
end
|
241
331
|
end
|
242
332
|
end
|
243
333
|
|
data/lib/libclimate/version.rb
CHANGED
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
#
|
3
|
+
# test Recls entries
|
4
|
+
|
5
|
+
$:.unshift File.join(File.dirname(__FILE__), '../..', 'lib')
|
6
|
+
|
7
|
+
|
8
|
+
require 'libclimate'
|
9
|
+
|
10
|
+
require 'xqsr3/extensions/test/unit'
|
11
|
+
|
12
|
+
require 'test/unit'
|
13
|
+
|
14
|
+
require 'stringio'
|
15
|
+
|
16
|
+
class Test_Climate_with_blocks < Test::Unit::TestCase
|
17
|
+
|
18
|
+
def test_flag_with_block
|
19
|
+
|
20
|
+
is_verbose = false
|
21
|
+
|
22
|
+
climate = LibCLImate::Climate.new do |climate|
|
23
|
+
|
24
|
+
climate.aliases << CLASP.Flag('--verbose') { is_verbose = true }
|
25
|
+
end
|
26
|
+
|
27
|
+
argv = %w{ --verbose }
|
28
|
+
|
29
|
+
climate.run argv
|
30
|
+
|
31
|
+
assert is_verbose, "block associated with flag '--verbose' was not executed"
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_option_with_block
|
35
|
+
|
36
|
+
flavour = nil
|
37
|
+
|
38
|
+
climate = LibCLImate::Climate.new do |climate|
|
39
|
+
|
40
|
+
climate.aliases << CLASP.Option('--flavour') { |o| flavour = o.value }
|
41
|
+
end
|
42
|
+
|
43
|
+
argv = %w{ --flavour=blueberry }
|
44
|
+
|
45
|
+
climate.run argv
|
46
|
+
|
47
|
+
assert_equal 'blueberry', flavour
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# ############################## end of file ############################# #
|
52
|
+
|
53
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: libclimate-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Wilson
|
@@ -66,6 +66,7 @@ files:
|
|
66
66
|
- test/scratch/blankzeroes.rb
|
67
67
|
- test/unit/tc_minimal.rb
|
68
68
|
- test/unit/tc_test_aliases.rb
|
69
|
+
- test/unit/tc_with_blocks.rb
|
69
70
|
- test/unit/ts_all.rb
|
70
71
|
homepage: http://www.libclimate.org/
|
71
72
|
licenses:
|