@@ -28,7 +28,7 @@ class ConsoleOptionParserTest extends CakeTestCase {
28
28
* @return void
29
29
*/
30
30
function testDescription () {
31
- $ parser = new ConsoleOptionParser ();
31
+ $ parser = new ConsoleOptionParser (' test ' , false );
32
32
$ result = $ parser ->description ('A test ' );
33
33
34
34
$ this ->assertEquals ($ parser , $ result , 'Setting description is not chainable ' );
@@ -44,7 +44,7 @@ function testDescription() {
44
44
* @return void
45
45
*/
46
46
function testEpilog () {
47
- $ parser = new ConsoleOptionParser ();
47
+ $ parser = new ConsoleOptionParser (' test ' , false );
48
48
$ result = $ parser ->epilog ('A test ' );
49
49
50
50
$ this ->assertEquals ($ parser , $ result , 'Setting epilog is not chainable ' );
@@ -60,7 +60,7 @@ function testEpilog() {
60
60
* @return void
61
61
*/
62
62
function testAddOptionReturnSelf () {
63
- $ parser = new ConsoleOptionParser ();
63
+ $ parser = new ConsoleOptionParser (' test ' , false );
64
64
$ result = $ parser ->addOption ('test ' );
65
65
$ this ->assertEquals ($ parser , $ result , 'Did not return $this from addOption ' );
66
66
}
@@ -71,12 +71,12 @@ function testAddOptionReturnSelf() {
71
71
* @return void
72
72
*/
73
73
function testAddOptionLong () {
74
- $ parser = new ConsoleOptionParser ();
74
+ $ parser = new ConsoleOptionParser (' test ' , false );
75
75
$ parser ->addOption ('test ' , array (
76
76
'short ' => 't '
77
77
));
78
78
$ result = $ parser ->parse (array ('--test ' , 'value ' ));
79
- $ this ->assertEquals (array ('test ' => 'value ' ), $ result [0 ], 'Long parameter did not parse out ' );
79
+ $ this ->assertEquals (array ('test ' => 'value ' , ' help ' => false ), $ result [0 ], 'Long parameter did not parse out ' );
80
80
}
81
81
82
82
/**
@@ -85,12 +85,12 @@ function testAddOptionLong() {
85
85
* @return void
86
86
*/
87
87
function testAddOptionLongEquals () {
88
- $ parser = new ConsoleOptionParser ();
88
+ $ parser = new ConsoleOptionParser (' test ' , false );
89
89
$ parser ->addOption ('test ' , array (
90
90
'short ' => 't '
91
91
));
92
92
$ result = $ parser ->parse (array ('--test=value ' ));
93
- $ this ->assertEquals (array ('test ' => 'value ' ), $ result [0 ], 'Long parameter did not parse out ' );
93
+ $ this ->assertEquals (array ('test ' => 'value ' , ' help ' => false ), $ result [0 ], 'Long parameter did not parse out ' );
94
94
}
95
95
96
96
/**
@@ -99,19 +99,19 @@ function testAddOptionLongEquals() {
99
99
* @return void
100
100
*/
101
101
function testAddOptionDefault () {
102
- $ parser = new ConsoleOptionParser ();
102
+ $ parser = new ConsoleOptionParser (' test ' , false );
103
103
$ parser ->addOption ('test ' , array (
104
104
'default ' => 'default value ' ,
105
105
));
106
106
$ result = $ parser ->parse (array ('--test ' ));
107
- $ this ->assertEquals (array ('test ' => 'default value ' ), $ result [0 ], 'Default value did not parse out ' );
107
+ $ this ->assertEquals (array ('test ' => 'default value ' , ' help ' => false ), $ result [0 ], 'Default value did not parse out ' );
108
108
109
- $ parser = new ConsoleOptionParser ();
109
+ $ parser = new ConsoleOptionParser (' test ' , false );
110
110
$ parser ->addOption ('test ' , array (
111
111
'default ' => 'default value ' ,
112
112
));
113
113
$ result = $ parser ->parse (array ());
114
- $ this ->assertEquals (array ('test ' => 'default value ' ), $ result [0 ], 'Default value did not parse out ' );
114
+ $ this ->assertEquals (array ('test ' => 'default value ' , ' help ' => false ), $ result [0 ], 'Default value did not parse out ' );
115
115
}
116
116
117
117
/**
@@ -120,12 +120,32 @@ function testAddOptionDefault() {
120
120
* @return void
121
121
*/
122
122
function testAddOptionShort () {
123
- $ parser = new ConsoleOptionParser ();
123
+ $ parser = new ConsoleOptionParser (' test ' , false );
124
124
$ parser ->addOption ('test ' , array (
125
125
'short ' => 't '
126
126
));
127
127
$ result = $ parser ->parse (array ('-t ' , 'value ' ));
128
- $ this ->assertEquals (array ('test ' => 'value ' ), $ result [0 ], 'Short parameter did not parse out ' );
128
+ $ this ->assertEquals (array ('test ' => 'value ' , 'help ' => false ), $ result [0 ], 'Short parameter did not parse out ' );
129
+ }
130
+
131
+ /**
132
+ * test adding and using boolean options.
133
+ *
134
+ * @return void
135
+ */
136
+ function testAddOptionBoolean () {
137
+ $ parser = new ConsoleOptionParser ('test ' , false );
138
+ $ parser ->addOption ('test ' , array (
139
+ 'boolean ' => true ,
140
+ ));
141
+
142
+ $ result = $ parser ->parse (array ('--test ' , 'value ' ));
143
+ $ expected = array (array ('test ' => true , 'help ' => false ), array ('value ' ));
144
+ $ this ->assertEquals ($ expected , $ result );
145
+
146
+ $ result = $ parser ->parse (array ('value ' ));
147
+ $ expected = array (array ('test ' => false , 'help ' => false ), array ('value ' ));
148
+ $ this ->assertEquals ($ expected , $ result );
129
149
}
130
150
131
151
/**
@@ -134,13 +154,13 @@ function testAddOptionShort() {
134
154
* @return void
135
155
*/
136
156
function testAddOptionMultipleShort () {
137
- $ parser = new ConsoleOptionParser ();
157
+ $ parser = new ConsoleOptionParser (' test ' , false );
138
158
$ parser ->addOption ('test ' , array ('short ' => 't ' ))
139
159
->addOption ('file ' , array ('short ' => 'f ' ))
140
160
->addOption ('output ' , array ('short ' => 'o ' ));
141
161
142
162
$ result = $ parser ->parse (array ('-o ' , '-t ' , '-f ' ));
143
- $ expected = array ('file ' => true , 'test ' => true , 'output ' => true );
163
+ $ expected = array ('file ' => true , 'test ' => true , 'output ' => true , ' help ' => false );
144
164
$ this ->assertEquals ($ expected , $ result [0 ], 'Short parameter did not parse out ' );
145
165
146
166
$ result = $ parser ->parse (array ('-otf ' ));
@@ -153,13 +173,13 @@ function testAddOptionMultipleShort() {
153
173
* @return void
154
174
*/
155
175
function testMultipleOptions () {
156
- $ parser = new ConsoleOptionParser ();
176
+ $ parser = new ConsoleOptionParser (' test ' , false );
157
177
$ parser ->addOption ('test ' )
158
178
->addOption ('connection ' )
159
179
->addOption ('table ' , array ('short ' => 't ' ));
160
180
161
181
$ result = $ parser ->parse (array ('--test ' , 'value ' , '-t ' , '--connection ' , 'postgres ' ));
162
- $ expected = array ('test ' => 'value ' , 'table ' => true , 'connection ' => 'postgres ' );
182
+ $ expected = array ('test ' => 'value ' , 'table ' => true , 'connection ' => 'postgres ' , ' help ' => false );
163
183
$ this ->assertEquals ($ expected , $ result [0 ], 'multiple options did not parse ' );
164
184
}
165
185
@@ -186,12 +206,12 @@ function testAddOptions() {
186
206
* @return void
187
207
*/
188
208
function testOptionWithBooleanParam () {
189
- $ parser = new ConsoleOptionParser ();
209
+ $ parser = new ConsoleOptionParser (' test ' , false );
190
210
$ parser ->addOption ('no-commit ' , array ('boolean ' => true ))
191
211
->addOption ('table ' , array ('short ' => 't ' ));
192
212
193
213
$ result = $ parser ->parse (array ('--table ' , 'posts ' , '--no-commit ' , 'arg1 ' , 'arg2 ' ));
194
- $ expected = array (array ('table ' => 'posts ' , 'no-commit ' => true ), array ('arg1 ' , 'arg2 ' ));
214
+ $ expected = array (array ('table ' => 'posts ' , 'no-commit ' => true , ' help ' => false ), array ('arg1 ' , 'arg2 ' ));
195
215
$ this ->assertEquals ($ expected , $ result , 'Boolean option did not parse correctly. ' );
196
216
}
197
217
@@ -201,7 +221,7 @@ function testOptionWithBooleanParam() {
201
221
* @expectedException InvalidArgumentException
202
222
*/
203
223
function testOptionThatDoesNotExist () {
204
- $ parser = new ConsoleOptionParser ();
224
+ $ parser = new ConsoleOptionParser (' test ' , false );
205
225
$ parser ->addOption ('no-commit ' , array ('boolean ' => true ));
206
226
207
227
$ result = $ parser ->parse (array ('--fail ' , 'other ' ));
@@ -214,11 +234,11 @@ function testOptionThatDoesNotExist() {
214
234
* @return void
215
235
*/
216
236
function testOptionWithChoices () {
217
- $ parser = new ConsoleOptionParser ();
237
+ $ parser = new ConsoleOptionParser (' test ' , false );
218
238
$ parser ->addOption ('name ' , array ('choices ' => array ('mark ' , 'jose ' )));
219
239
220
240
$ result = $ parser ->parse (array ('--name ' , 'mark ' ));
221
- $ expected = array ('name ' => 'mark ' );
241
+ $ expected = array ('name ' => 'mark ' , ' help ' => false );
222
242
$ this ->assertEquals ($ expected , $ result [0 ], 'Got the correct value. ' );
223
243
224
244
$ result = $ parser ->parse (array ('--name ' , 'jimmy ' ));
@@ -230,7 +250,7 @@ function testOptionWithChoices() {
230
250
* @return void
231
251
*/
232
252
function testPositionalArgument () {
233
- $ parser = new ConsoleOptionParser ();
253
+ $ parser = new ConsoleOptionParser (' test ' , false );
234
254
$ result = $ parser ->addArgument ('name ' , array ('help ' => 'An argument ' ));
235
255
$ this ->assertEquals ($ parser , $ result , 'Should returnn this ' );
236
256
}
@@ -241,7 +261,7 @@ function testPositionalArgument() {
241
261
* @return void
242
262
*/
243
263
function testPositionalArgOverwrite () {
244
- $ parser = new ConsoleOptionParser ();
264
+ $ parser = new ConsoleOptionParser (' test ' , false );
245
265
$ parser ->addArgument ('name ' , array ('help ' => 'An argument ' ))
246
266
->addArgument ('other ' , array ('index ' => 0 ));
247
267
@@ -256,7 +276,7 @@ function testPositionalArgOverwrite() {
256
276
* @return void
257
277
*/
258
278
function testParseArgumentTooMany () {
259
- $ parser = new ConsoleOptionParser ();
279
+ $ parser = new ConsoleOptionParser (' test ' , false );
260
280
$ parser ->addArgument ('name ' , array ('help ' => 'An argument ' ))
261
281
->addArgument ('other ' );
262
282
@@ -274,7 +294,7 @@ function testParseArgumentTooMany() {
274
294
* @return void
275
295
*/
276
296
function testPositionalArgNotEnough () {
277
- $ parser = new ConsoleOptionParser ();
297
+ $ parser = new ConsoleOptionParser (' test ' , false );
278
298
$ parser ->addArgument ('name ' , array ('required ' => true ))
279
299
->addArgument ('other ' , array ('required ' => true ));
280
300
@@ -288,7 +308,7 @@ function testPositionalArgNotEnough() {
288
308
* @return void
289
309
*/
290
310
function testPositionalArgWithChoices () {
291
- $ parser = new ConsoleOptionParser ();
311
+ $ parser = new ConsoleOptionParser (' test ' , false );
292
312
$ parser ->addArgument ('name ' , array ('choices ' => array ('mark ' , 'jose ' )))
293
313
->addArgument ('alias ' , array ('choices ' => array ('cowboy ' , 'samurai ' )))
294
314
->addArgument ('weapon ' , array ('choices ' => array ('gun ' , 'sword ' )));
@@ -306,7 +326,7 @@ function testPositionalArgWithChoices() {
306
326
* @return void
307
327
*/
308
328
function testAddArguments () {
309
- $ parser = new ConsoleOptionParser ();
329
+ $ parser = new ConsoleOptionParser (' test ' , false );
310
330
$ result = $ parser ->addArguments (array (
311
331
'name ' => array ('help ' => 'The name ' ),
312
332
'other ' => array ('help ' => 'The other arg ' )
@@ -323,7 +343,7 @@ function testAddArguments() {
323
343
* @return void
324
344
*/
325
345
function testSubcommand () {
326
- $ parser = new ConsoleOptionParser ();
346
+ $ parser = new ConsoleOptionParser (' test ' , false );
327
347
$ result = $ parser ->addSubcommand ('initdb ' , array (
328
348
'help ' => 'Initialize the database '
329
349
));
@@ -336,7 +356,7 @@ function testSubcommand() {
336
356
* @return void
337
357
*/
338
358
function testAddSubcommands () {
339
- $ parser = new ConsoleOptionParser ();
359
+ $ parser = new ConsoleOptionParser (' test ' , false );
340
360
$ result = $ parser ->addSubcommands (array (
341
361
'initdb ' => array ('help ' => 'Initialize the database ' ),
342
362
'create ' => array ('help ' => 'Create something ' )
@@ -589,7 +609,7 @@ function testBuildFromArray() {
589
609
* @return void
590
610
*/
591
611
function testParsingWithSubParser () {
592
- $ parser = new ConsoleOptionParser ();
612
+ $ parser = new ConsoleOptionParser (' test ' , false );
593
613
$ parser ->addOption ('primary ' )
594
614
->addArgument ('one ' , array ('required ' => true , 'choices ' => array ('a ' , 'b ' )))
595
615
->addArgument ('two ' , array ('required ' => true ))
@@ -606,7 +626,12 @@ function testParsingWithSubParser() {
606
626
));
607
627
608
628
$ result = $ parser ->parse (array ('--secondary ' , '--fourth ' , '4 ' , 'c ' ), 'sub ' );
609
- $ expected = array (array ('secondary ' => true , 'fourth ' => '4 ' ), array ('c ' ));
629
+ $ expected = array (array (
630
+ 'secondary ' => true ,
631
+ 'fourth ' => '4 ' ,
632
+ 'help ' => false ,
633
+ 'verbose ' => false ,
634
+ 'quiet ' => false ), array ('c ' ));
610
635
$ this ->assertEquals ($ expected , $ result , 'Sub parser did not parse request. ' );
611
636
}
612
637
}
0 commit comments