choosy 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +229 -221
- data/Rakefile +21 -3
- data/examples/bar.rb +44 -0
- data/examples/foo.rb +198 -0
- data/examples/superfoo.rb +125 -0
- data/lib/VERSION +1 -1
- data/lib/choosy/argument.rb +51 -0
- data/lib/choosy/base_command.rb +22 -7
- data/lib/choosy/command.rb +12 -4
- data/lib/choosy/dsl/argument_builder.rb +88 -0
- data/lib/choosy/dsl/base_command_builder.rb +71 -56
- data/lib/choosy/dsl/command_builder.rb +14 -2
- data/lib/choosy/dsl/option_builder.rb +43 -83
- data/lib/choosy/dsl/super_command_builder.rb +37 -9
- data/lib/choosy/option.rb +13 -11
- data/lib/choosy/parse_result.rb +8 -27
- data/lib/choosy/parser.rb +20 -16
- data/lib/choosy/printing/color.rb +39 -21
- data/lib/choosy/printing/erb_printer.rb +12 -3
- data/lib/choosy/printing/formatting_element.rb +17 -0
- data/lib/choosy/printing/help_printer.rb +204 -117
- data/lib/choosy/printing/terminal.rb +53 -0
- data/lib/choosy/super_command.rb +6 -6
- data/lib/choosy/super_parser.rb +26 -15
- data/lib/choosy/verifier.rb +61 -6
- data/spec/choosy/base_command_spec.rb +27 -2
- data/spec/choosy/command_spec.rb +31 -9
- data/spec/choosy/dsl/argument_builder_spec.rb +180 -0
- data/spec/choosy/dsl/base_command_builder_spec.rb +87 -44
- data/spec/choosy/dsl/commmand_builder_spec.rb +15 -4
- data/spec/choosy/dsl/option_builder_spec.rb +101 -191
- data/spec/choosy/dsl/super_command_builder_spec.rb +34 -9
- data/spec/choosy/parser_spec.rb +30 -8
- data/spec/choosy/printing/color_spec.rb +19 -5
- data/spec/choosy/printing/help_printer_spec.rb +152 -73
- data/spec/choosy/printing/terminal_spec.rb +27 -0
- data/spec/choosy/super_command_spec.rb +17 -17
- data/spec/choosy/super_parser_spec.rb +20 -10
- data/spec/choosy/verifier_spec.rb +137 -47
- data/spec/integration/command-A_spec.rb +6 -6
- data/spec/integration/command-B_spec.rb +45 -0
- data/spec/integration/supercommand-A_spec.rb +33 -27
- data/spec/integration/supercommand-B_spec.rb +32 -0
- data/spec/spec_helpers.rb +8 -5
- metadata +95 -54
@@ -21,9 +21,31 @@ module Choosy::DSL
|
|
21
21
|
@command.printer.should be_a(Choosy::Printing::HelpPrinter)
|
22
22
|
end
|
23
23
|
|
24
|
+
it "should turn on color by default" do
|
25
|
+
@builder.printer :standard
|
26
|
+
@command.printer.color.should_not be_disabled
|
27
|
+
end
|
28
|
+
|
24
29
|
it "should know how to turn off color" do
|
25
30
|
@builder.printer :standard, :color => false
|
26
|
-
@command.printer.color.
|
31
|
+
@command.printer.color.should be_disabled
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should know how to set the maximum width" do
|
35
|
+
@builder.printer :standard, :max_width => 70
|
36
|
+
@command.printer.columns.should eql(70)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should know how to set the header attributes" do
|
40
|
+
@builder.printer :standard, :header_styles => [:bold, :green]
|
41
|
+
@command.printer.header_styles.should eql([:bold, :green])
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should be able to set multiple properties of the printer" do
|
45
|
+
@builder.printer :standard, :max_width => 25, :header_styles => [:bold, :red], :color => false
|
46
|
+
@command.printer.color.should be_disabled
|
47
|
+
@command.printer.header_styles.should eql([:bold, :red])
|
48
|
+
@command.printer.columns.should eql(25)
|
27
49
|
end
|
28
50
|
|
29
51
|
class TestPrinter
|
@@ -47,6 +69,7 @@ module Choosy::DSL
|
|
47
69
|
it "should be able to handle a given template" do
|
48
70
|
@builder.printer :erb, :template => __FILE__
|
49
71
|
@command.printer.should be_a(Choosy::Printing::ERBPrinter)
|
72
|
+
@command.printer.template.should eql(__FILE__)
|
50
73
|
end
|
51
74
|
|
52
75
|
it "should fail when the tempate file doesn't exist" do
|
@@ -70,41 +93,49 @@ module Choosy::DSL
|
|
70
93
|
end
|
71
94
|
end#summary
|
72
95
|
|
73
|
-
describe :
|
74
|
-
it "should set
|
75
|
-
@builder.
|
76
|
-
@command.
|
96
|
+
describe :header do
|
97
|
+
it "should set a header for this command" do
|
98
|
+
@builder.header 'HEADER'
|
99
|
+
@command.listing[0].value.should eql('HEADER')
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should set the attributes of the header effectively" do
|
103
|
+
@builder.header 'HEADER', :bold, :blue
|
104
|
+
@command.listing[0].styles.should eql([:bold, :blue])
|
77
105
|
end
|
78
|
-
end#
|
106
|
+
end#header
|
79
107
|
|
80
|
-
describe :
|
81
|
-
it "should add a
|
82
|
-
@builder.
|
83
|
-
@command.listing[0].should eql('Required arguments')
|
108
|
+
describe :para do
|
109
|
+
it "should add a paragraph to the list of options for printing" do
|
110
|
+
@builder.para 'Required arguments'
|
111
|
+
@command.listing[0].value.should eql('Required arguments')
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should leave the paragraph without attributes" do
|
115
|
+
@builder.para 'No attributes'
|
116
|
+
@command.listing[0].styles.should eql([])
|
84
117
|
end
|
85
118
|
|
86
119
|
it "should add an empty string to the listing when called with no arguments" do
|
87
|
-
@builder.
|
88
|
-
@command.listing[0].should
|
120
|
+
@builder.para
|
121
|
+
@command.listing[0].value.should be(nil)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should add attributes to the paragraph" do
|
125
|
+
@builder.para 'Here', :bold, :red
|
126
|
+
@command.listing[0].styles.should eql([:bold, :red])
|
89
127
|
end
|
90
|
-
end#
|
128
|
+
end#para
|
91
129
|
|
92
130
|
describe :option do
|
93
131
|
describe "when using just a name" do
|
94
132
|
it "should set the name of the option" do
|
95
|
-
o = @builder.option :opto do
|
96
|
-
|
133
|
+
o = @builder.option :opto do
|
134
|
+
short '-o'
|
97
135
|
end
|
98
136
|
o.name.should eql(:opto)
|
99
137
|
end
|
100
138
|
|
101
|
-
it "should handle a CommandBuilder block" do
|
102
|
-
@builder.option :opto do |o|
|
103
|
-
o.short '-s'
|
104
|
-
o.should be_a(OptionBuilder)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
139
|
it "should fail when no block is given" do
|
109
140
|
attempting {
|
110
141
|
@builder.option "blah"
|
@@ -113,8 +144,8 @@ module Choosy::DSL
|
|
113
144
|
|
114
145
|
it "should fail when the symbol is nil" do
|
115
146
|
attempting {
|
116
|
-
@builder.option nil do
|
117
|
-
|
147
|
+
@builder.option nil do
|
148
|
+
short = '-o'
|
118
149
|
end
|
119
150
|
}.should raise_error(Choosy::ConfigurationError, /The option name was nil/)
|
120
151
|
end
|
@@ -123,10 +154,10 @@ module Choosy::DSL
|
|
123
154
|
describe "when using a hash" do
|
124
155
|
describe "for dependencies" do
|
125
156
|
it "should set the dependencies of that option" do
|
126
|
-
o = @builder.option :o => [:s] do
|
127
|
-
|
157
|
+
o = @builder.option :o => [:s] do
|
158
|
+
short '-o'
|
128
159
|
end
|
129
|
-
o.dependent_options.should have(1).
|
160
|
+
o.dependent_options.should have(1).item
|
130
161
|
o.dependent_options[0].should eql(:s)
|
131
162
|
end
|
132
163
|
end
|
@@ -143,8 +174,8 @@ module Choosy::DSL
|
|
143
174
|
end
|
144
175
|
|
145
176
|
it "should still accept the block" do
|
146
|
-
o = @builder.option :o => {:short => '-o'} do
|
147
|
-
|
177
|
+
o = @builder.option :o => {:short => '-o'} do
|
178
|
+
desc "short"
|
148
179
|
end
|
149
180
|
o.description.should eql('short')
|
150
181
|
end
|
@@ -172,8 +203,8 @@ module Choosy::DSL
|
|
172
203
|
|
173
204
|
describe "after the option block has been processed" do
|
174
205
|
it "should call the 'finalize!' method on the builder" do
|
175
|
-
o = @builder.option :o do
|
176
|
-
|
206
|
+
o = @builder.option :o do
|
207
|
+
short '-o'
|
177
208
|
end
|
178
209
|
o.cast_to.should eql(:boolean)
|
179
210
|
end
|
@@ -219,8 +250,8 @@ module Choosy::DSL
|
|
219
250
|
end
|
220
251
|
|
221
252
|
it "should be able to capture block level data" do
|
222
|
-
o = @builder.boolean :debug, "Show debug output" do
|
223
|
-
|
253
|
+
o = @builder.boolean :debug, "Show debug output" do
|
254
|
+
short '-D'
|
224
255
|
end
|
225
256
|
o.short_flag.should eql("-D")
|
226
257
|
end
|
@@ -229,17 +260,29 @@ module Choosy::DSL
|
|
229
260
|
o = @builder.boolean_ :debug, "Show debug output"
|
230
261
|
o.short_flag.should be(nil)
|
231
262
|
end
|
263
|
+
|
264
|
+
it "should set the default value if not set" do
|
265
|
+
o = @builder.boolean :debug, "Show debug"
|
266
|
+
o.default_value.should be(false)
|
267
|
+
end
|
232
268
|
end#boolean
|
233
269
|
|
270
|
+
describe :enum do
|
271
|
+
it "should set the allowable values" do
|
272
|
+
o = @builder.enum :this, [:a, :b, :c], "this"
|
273
|
+
o.allowable_values.should eql([:a, :b, :c])
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
234
277
|
describe :single do
|
235
278
|
it "should be able to set the short flag" do
|
236
279
|
o = @builder.single :count, "Show the count"
|
237
280
|
o.short_flag.should eql('-c')
|
238
281
|
end
|
239
282
|
|
240
|
-
it "should be able to set the
|
283
|
+
it "should be able to set the metaname name" do
|
241
284
|
o = @builder.single :count, "Show the count"
|
242
|
-
o.
|
285
|
+
o.metaname.should eql('COUNT')
|
243
286
|
end
|
244
287
|
end#single
|
245
288
|
|
@@ -249,14 +292,14 @@ module Choosy::DSL
|
|
249
292
|
o.short_flag.should eql('-f')
|
250
293
|
end
|
251
294
|
|
252
|
-
it "should be able to set the
|
295
|
+
it "should be able to set the metaname name" do
|
253
296
|
o = @builder.multiple :file_names, "The file names"
|
254
|
-
o.
|
297
|
+
o.metaname.should eql('FILE_NAMES+')
|
255
298
|
end
|
256
299
|
|
257
300
|
it "should be able to yield a block" do
|
258
|
-
o = @builder.multiple :file_names, "The file names" do
|
259
|
-
|
301
|
+
o = @builder.multiple :file_names, "The file names" do
|
302
|
+
long '--files'
|
260
303
|
end
|
261
304
|
o.long_flag.should eql('--files')
|
262
305
|
end
|
@@ -268,9 +311,9 @@ module Choosy::DSL
|
|
268
311
|
o = @builder.send(method, method, "Desc of #{method}")
|
269
312
|
o.cast_to.should eql(Choosy::Converter.for(method))
|
270
313
|
if o.cast_to == :boolean
|
271
|
-
o.
|
314
|
+
o.metaname.should be(nil)
|
272
315
|
else
|
273
|
-
o.
|
316
|
+
o.metaname.should eql(method.to_s.upcase)
|
274
317
|
end
|
275
318
|
end
|
276
319
|
|
@@ -286,7 +329,7 @@ module Choosy::DSL
|
|
286
329
|
plural = "#{method}s".to_sym
|
287
330
|
o = @builder.send(plural, plural, "Desc of #{plural}")
|
288
331
|
o.cast_to.should eql(Choosy::Converter.for(method))
|
289
|
-
o.
|
332
|
+
o.metaname.should eql("#{plural.to_s.upcase}+")
|
290
333
|
end
|
291
334
|
|
292
335
|
it "#{method}s_" do
|
@@ -306,8 +349,8 @@ module Choosy::DSL
|
|
306
349
|
end
|
307
350
|
|
308
351
|
it "should allow you to use a block to alter the help message" do
|
309
|
-
v = @builder.version 'blah' do
|
310
|
-
|
352
|
+
v = @builder.version 'blah' do
|
353
|
+
desc "Version"
|
311
354
|
end
|
312
355
|
v.description.should eql("Version")
|
313
356
|
end
|
@@ -66,14 +66,25 @@ module Choosy::DSL
|
|
66
66
|
it "should fail if there is no block given" do
|
67
67
|
attempting {
|
68
68
|
@builder.arguments
|
69
|
-
}.
|
69
|
+
}.should_not raise_error
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should pass in the block correctly" do
|
73
|
+
@builder.arguments do
|
74
|
+
metaname 'ARGS'
|
75
|
+
end
|
76
|
+
@command.arguments.metaname.should eql('ARGS')
|
70
77
|
end
|
71
78
|
|
72
79
|
it "should pass in the arguments to validate" do
|
73
|
-
@builder.arguments do
|
74
|
-
args
|
80
|
+
@builder.arguments do
|
81
|
+
validate do |args, options|
|
82
|
+
raise RuntimeError, "called"
|
83
|
+
end
|
75
84
|
end
|
76
|
-
|
85
|
+
attempting {
|
86
|
+
@command.arguments.validation_step.call([2, 2, 3], nil)
|
87
|
+
}.should raise_error(RuntimeError, "called")
|
77
88
|
end
|
78
89
|
end#arguments
|
79
90
|
end
|
@@ -21,18 +21,18 @@ module Choosy::DSL
|
|
21
21
|
end
|
22
22
|
|
23
23
|
describe "and the arity" do
|
24
|
-
it "should not be set without a
|
24
|
+
it "should not be set without a metaname" do
|
25
25
|
@builder.short '-s'
|
26
26
|
@option.short_flag.should eql('-s')
|
27
27
|
@option.arity.should be(nil)
|
28
28
|
end
|
29
29
|
|
30
|
-
it "should be set on STD+
|
30
|
+
it "should be set on STD+ metaname" do
|
31
31
|
@builder.short '-s', 'STD+'
|
32
32
|
@option.arity.should eql(1..1000)
|
33
33
|
end
|
34
34
|
|
35
|
-
it "should set single arity on STD
|
35
|
+
it "should set single arity on STD metaname" do
|
36
36
|
@builder.short '-s', 'STD'
|
37
37
|
@option.arity.should eql(1..1)
|
38
38
|
end
|
@@ -44,15 +44,15 @@ module Choosy::DSL
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
describe "and the flag
|
48
|
-
it "on STD
|
47
|
+
describe "and the flag metaname" do
|
48
|
+
it "on STD metanames" do
|
49
49
|
@builder.short '-s', 'STD'
|
50
|
-
@option.
|
50
|
+
@option.metaname.should eql('STD')
|
51
51
|
end
|
52
52
|
|
53
|
-
it "on STD+
|
53
|
+
it "on STD+ metanames" do
|
54
54
|
@builder.short '-s', 'STD+'
|
55
|
-
@option.
|
55
|
+
@option.metaname.should eql('STD+')
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end#short
|
@@ -80,15 +80,15 @@ module Choosy::DSL
|
|
80
80
|
end
|
81
81
|
end#arity
|
82
82
|
|
83
|
-
describe "and the flag
|
84
|
-
it "on STD
|
83
|
+
describe "and the flag metaname" do
|
84
|
+
it "on STD metanames" do
|
85
85
|
@builder.long '-s', 'STD'
|
86
|
-
@option.
|
86
|
+
@option.metaname.should eql('STD')
|
87
87
|
end
|
88
88
|
|
89
|
-
it "on STD+
|
89
|
+
it "on STD+ metanames" do
|
90
90
|
@builder.long '-s', 'STD+'
|
91
|
-
@option.
|
91
|
+
@option.metaname.should eql('STD+')
|
92
92
|
end
|
93
93
|
end
|
94
94
|
end#long
|
@@ -106,7 +106,7 @@ module Choosy::DSL
|
|
106
106
|
|
107
107
|
it "should leave the param nil when just the short flag is given" do
|
108
108
|
@builder.flags '-s'
|
109
|
-
@option.
|
109
|
+
@option.metaname.should be(nil)
|
110
110
|
end
|
111
111
|
|
112
112
|
it "should be able to set the short when the long flag is given" do
|
@@ -119,14 +119,14 @@ module Choosy::DSL
|
|
119
119
|
@option.long_flag.should eql('--short')
|
120
120
|
end
|
121
121
|
|
122
|
-
it "should leave the
|
122
|
+
it "should leave the metaname empty when not given" do
|
123
123
|
@builder.flags '-s', '--short'
|
124
|
-
@option.
|
124
|
+
@option.metaname.should be(nil)
|
125
125
|
end
|
126
126
|
|
127
|
-
it "should set the
|
127
|
+
it "should set the metaname if given" do
|
128
128
|
@builder.flags '-s', '--short', 'SHORT'
|
129
|
-
@option.
|
129
|
+
@option.metaname.should eql('SHORT')
|
130
130
|
end
|
131
131
|
end#flags
|
132
132
|
|
@@ -144,209 +144,69 @@ module Choosy::DSL
|
|
144
144
|
end
|
145
145
|
end#default
|
146
146
|
|
147
|
-
describe :
|
148
|
-
it "should set the option" do
|
149
|
-
@builder.required
|
150
|
-
@option.required?.should be(true)
|
151
|
-
end
|
152
|
-
|
153
|
-
it "should set the option on non-nil/non-true" do
|
154
|
-
@builder.required 1
|
155
|
-
@option.required?.should be(false)
|
156
|
-
end
|
157
|
-
|
158
|
-
it "should set the option on false" do
|
159
|
-
@builder.required false
|
160
|
-
@option.required?.should be(false)
|
161
|
-
end
|
162
|
-
end#required
|
163
|
-
|
164
|
-
describe :param do
|
165
|
-
it "should be able to set the name of the parameter" do
|
166
|
-
@builder.param 'PARAM'
|
167
|
-
@option.flag_parameter.should eql('PARAM')
|
168
|
-
end
|
169
|
-
|
170
|
-
it "should set the arity on STD+ to 1+" do
|
171
|
-
@builder.param 'STD+'
|
172
|
-
@option.arity.should eql(1..1000)
|
173
|
-
end
|
174
|
-
|
175
|
-
it "should set the arity on STD to 1" do
|
176
|
-
@builder.param 'STD'
|
177
|
-
@option.arity.should eql(1..1)
|
178
|
-
end
|
179
|
-
end#param
|
180
|
-
|
181
|
-
describe :count do
|
182
|
-
describe "when welformed" do
|
183
|
-
it "should set :at_least the right arity" do
|
184
|
-
@builder.count :at_least => 32
|
185
|
-
@option.arity.should eql(32..1000)
|
186
|
-
end
|
187
|
-
|
188
|
-
it "should set :at_most the right arity" do
|
189
|
-
@builder.count :at_most => 31
|
190
|
-
@option.arity.should eql(1..31)
|
191
|
-
end
|
192
|
-
|
193
|
-
it "should set :once to the right arity" do
|
194
|
-
@builder.count :once
|
195
|
-
@option.arity.should eql(1..1)
|
196
|
-
end
|
197
|
-
|
198
|
-
it "should set :zero to the right arity" do
|
199
|
-
@builder.count :zero
|
200
|
-
@option.arity.should eql(0..0)
|
201
|
-
end
|
202
|
-
|
203
|
-
it "should set :none to the right arity" do
|
204
|
-
@builder.count :none
|
205
|
-
@option.arity.should eql(0..0)
|
206
|
-
end
|
207
|
-
|
208
|
-
it "should set a number exactly" do
|
209
|
-
@builder.count 3
|
210
|
-
@option.arity.should eql(3..3)
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
describe "when malformed" do
|
215
|
-
it "should fail when the :exactly isn't a number" do
|
216
|
-
attempting {
|
217
|
-
@builder.count :exactly => 'p'
|
218
|
-
}.should raise_error(Choosy::ConfigurationError, /number/)
|
219
|
-
end
|
220
|
-
|
221
|
-
it "should fail when the :at_most isn't a number" do
|
222
|
-
attempting {
|
223
|
-
@builder.count :at_most => 'p'
|
224
|
-
}.should raise_error(Choosy::ConfigurationError, /number/)
|
225
|
-
end
|
226
|
-
|
227
|
-
it "should fail when the :at_least isn't a number" do
|
228
|
-
attempting {
|
229
|
-
@builder.count :at_least => 'p'
|
230
|
-
}.should raise_error(Choosy::ConfigurationError, /number/)
|
231
|
-
end
|
232
|
-
|
233
|
-
it "should fail when the :count isn't a number" do
|
234
|
-
attempting {
|
235
|
-
@builder.count 'p'
|
236
|
-
}.should raise_error(Choosy::ConfigurationError, /number/)
|
237
|
-
end
|
238
|
-
|
239
|
-
it "should fail when the :at_least is greater than :at_most" do
|
240
|
-
attempting {
|
241
|
-
@builder.count :at_least => 3, :at_most => 2
|
242
|
-
}.should raise_error(Choosy::ConfigurationError, /lower bound/)
|
243
|
-
end
|
244
|
-
end
|
245
|
-
end#count
|
246
|
-
|
247
|
-
describe :cast do
|
248
|
-
it "should allow symbol casts" do
|
249
|
-
@builder.cast :int
|
250
|
-
@option.cast_to.should eql(:integer)
|
251
|
-
end
|
252
|
-
|
253
|
-
class CustomConverter
|
254
|
-
def convert(value)
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
it "should allow for custom conversions" do
|
259
|
-
conv = CustomConverter.new
|
260
|
-
@builder.cast conv
|
261
|
-
@option.cast_to.should be(conv)
|
262
|
-
end
|
263
|
-
|
264
|
-
it "should fail if it doesn't know about a Type" do
|
265
|
-
attempting {
|
266
|
-
@builder.cast Choosy::Error
|
267
|
-
}.should raise_error(Choosy::ConfigurationError, /Unknown conversion/)
|
268
|
-
end
|
269
|
-
|
270
|
-
it "should fail if it doesn't know about a symbol" do
|
271
|
-
attempting {
|
272
|
-
@builder.cast :unknown_type
|
273
|
-
}.should raise_error(Choosy::ConfigurationError, /Unknown conversion/)
|
274
|
-
end
|
275
|
-
end#cast
|
276
|
-
|
277
|
-
describe :fail do
|
147
|
+
describe :die do
|
278
148
|
it "should format the error message with both flags" do
|
279
149
|
@builder.short '-k'
|
280
150
|
@builder.long '--keep', 'KEEP'
|
281
151
|
attempting {
|
282
|
-
@builder.
|
152
|
+
@builder.die "Didn't keep anything"
|
283
153
|
}.should raise_error(Choosy::ValidationError, /-k\/--keep KEEP: Didn't keep anything/)
|
284
154
|
end
|
285
155
|
|
286
156
|
it "should set the format of the error with the short flag" do
|
287
157
|
@builder.short '-k'
|
288
158
|
attempting {
|
289
|
-
@builder.
|
159
|
+
@builder.die "Didn't keep anything"
|
290
160
|
}.should raise_error(Choosy::ValidationError, /-k: Didn't keep anything/)
|
291
161
|
end
|
292
162
|
|
293
163
|
it "should set the format of the long flag alone" do
|
294
164
|
@builder.long '--keep'
|
295
165
|
attempting {
|
296
|
-
@builder.
|
166
|
+
@builder.die "Didn't keep anything"
|
297
167
|
}.should raise_error(Choosy::ValidationError, /--keep: Didn't keep anything/)
|
298
168
|
end
|
299
|
-
end#
|
169
|
+
end#die
|
300
170
|
|
301
|
-
describe :
|
302
|
-
it "should
|
303
|
-
@builder.
|
304
|
-
|
305
|
-
end
|
306
|
-
@option.validation_step.should be_a(Proc)
|
307
|
-
end
|
308
|
-
|
309
|
-
it "should have access to the larger context when called" do
|
310
|
-
value = nil
|
311
|
-
@builder.validate do
|
312
|
-
value = 'here'
|
313
|
-
end
|
314
|
-
@option.validation_step.call
|
315
|
-
value.should eql('here')
|
171
|
+
describe :depends_on do
|
172
|
+
it "should be able to process multiple arguments" do
|
173
|
+
@builder.depends_on :a, :b
|
174
|
+
@option.dependent_options.should eql([:a, :b])
|
316
175
|
end
|
317
|
-
end#validate
|
318
176
|
|
319
|
-
|
320
|
-
|
321
|
-
@
|
322
|
-
@builder.finalize!
|
323
|
-
@option.arity.should eql(0..0)
|
177
|
+
it "should be able to process Array arguments" do
|
178
|
+
@builder.depends_on [:a, :b]
|
179
|
+
@option.dependent_options.should eql([:a, :b])
|
324
180
|
end
|
181
|
+
end#depends_on
|
325
182
|
|
326
|
-
|
327
|
-
|
328
|
-
@builder.
|
329
|
-
@option.
|
183
|
+
describe :only do
|
184
|
+
it "should set the allowable_values for an option" do
|
185
|
+
@builder.only :this, :that, :other
|
186
|
+
@option.allowable_values.should eql([:this, :that, :other])
|
330
187
|
end
|
188
|
+
end#only
|
331
189
|
|
332
|
-
|
333
|
-
|
334
|
-
@builder.
|
335
|
-
@option.
|
190
|
+
describe :negate do
|
191
|
+
it "should set the default negation to 'no'" do
|
192
|
+
@builder.negate
|
193
|
+
@option.negated?.should be_true
|
194
|
+
@option.negation.should eql('no')
|
336
195
|
end
|
337
|
-
end#finalize!
|
338
196
|
|
339
|
-
|
340
|
-
|
341
|
-
@
|
342
|
-
@option.
|
197
|
+
it "should set the the negation to a specific value" do
|
198
|
+
@builder.negate 'non'
|
199
|
+
@option.negated?.should be_true
|
200
|
+
@option.negation.should eql('non')
|
343
201
|
end
|
344
202
|
|
345
|
-
it "should
|
346
|
-
@builder.
|
347
|
-
@
|
203
|
+
it "should set the flag string for the negated value" do
|
204
|
+
@builder.flags '-s', '--short'
|
205
|
+
@builder.negate
|
206
|
+
@builder.finalize!
|
207
|
+
@option.negated.should eql("--no-short")
|
348
208
|
end
|
349
|
-
end#
|
209
|
+
end#negate
|
350
210
|
|
351
211
|
describe :from_hash do
|
352
212
|
it "should fail on unrecognized methods" do
|
@@ -382,5 +242,55 @@ module Choosy::DSL
|
|
382
242
|
}.should raise_error(Choosy::ConfigurationError, /Only hash arguments allowed/)
|
383
243
|
end
|
384
244
|
end#from_hash
|
245
|
+
|
246
|
+
describe :finalize! do
|
247
|
+
it "should set the arity if not already set" do
|
248
|
+
@builder.short '-s'
|
249
|
+
@builder.finalize!
|
250
|
+
@option.arity.should eql(0..0)
|
251
|
+
end
|
252
|
+
|
253
|
+
it "should set the cast to :string on regular arguments" do
|
254
|
+
@builder.short '-s', 'SHORT'
|
255
|
+
@builder.finalize!
|
256
|
+
@option.cast_to.should eql(:string)
|
257
|
+
end
|
258
|
+
|
259
|
+
it "should set the cast to :boolean on single flags" do
|
260
|
+
@builder.short '-s'
|
261
|
+
@builder.finalize!
|
262
|
+
@option.cast_to.should eql(:boolean)
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should fail when both boolean and restricted" do
|
266
|
+
@builder.short '-s'
|
267
|
+
@builder.only :a, :b
|
268
|
+
attempting{
|
269
|
+
@builder.finalize!
|
270
|
+
}.should raise_error(Choosy::ConfigurationError, /boolean and restricted/)
|
271
|
+
end
|
272
|
+
|
273
|
+
it "should fail when the argument is negated and not boolean" do
|
274
|
+
@builder.short '-s', 'SHORT'
|
275
|
+
@builder.negate
|
276
|
+
attempting {
|
277
|
+
@builder.finalize!
|
278
|
+
}.should raise_error(Choosy::ConfigurationError, /negate a non-boolean option/)
|
279
|
+
end
|
280
|
+
|
281
|
+
it "should fail when there is no long boolean option name to negate" do
|
282
|
+
@builder.short '-s'
|
283
|
+
@builder.negate
|
284
|
+
attempting {
|
285
|
+
@builder.finalize!
|
286
|
+
}.should raise_error(Choosy::ConfigurationError, /long flag is required for negation/)
|
287
|
+
end
|
288
|
+
|
289
|
+
it "should set the default value for booleans if not already set" do
|
290
|
+
@builder.short '-s'
|
291
|
+
@builder.finalize!
|
292
|
+
@option.default_value.should be(false)
|
293
|
+
end
|
294
|
+
end#finalize!
|
385
295
|
end
|
386
296
|
end
|