Skip to content

Commit 1009069

Browse files
committed
Making boolean switches behave.
Boolean switches always show up in the parsed options. When left undefined they insert a false, and when included they insert a true. This makes working with them require less checks.
1 parent b328276 commit 1009069

File tree

4 files changed

+70
-36
lines changed

4 files changed

+70
-36
lines changed

cake/console/console_option_parser.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,15 @@ public function parse($argv, $command = null) {
445445
}
446446
}
447447
foreach ($this->_options as $option) {
448-
if ($option->defaultValue() !== null && !isset($params[$option->name()]) && !$option->isBoolean()) {
449-
$params[$option->name()] = $option->defaultValue();
448+
$name = $option->name();
449+
$isBoolean = $option->isBoolean();
450+
$default = $option->defaultValue();
451+
452+
if ($default !== null && !isset($params[$name]) && !$isBoolean) {
453+
$params[$name] = $default;
454+
}
455+
if ($isBoolean && !isset($params[$name])) {
456+
$params[$name] = false;
450457
}
451458
}
452459
return array($params, $args);

cake/console/libs/shell.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public function runCommand($command, $argv) {
331331
if (($isTask || $isMethod || $isMain) && $command !== 'execute' ) {
332332
$this->startup();
333333
}
334-
if (isset($this->params['help'])) {
334+
if (!empty($this->params['help'])) {
335335
return $this->out($this->OptionParser->help($command));
336336
}
337337

cake/console/libs/testsuite.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ protected function parseArgs() {
104104
*/
105105
protected function runnerOptions() {
106106
$options = array();
107-
foreach ($this->params as $param => $value) {
107+
$params = $this->params;
108+
unset($params['help']);
109+
foreach ($params as $param => $value) {
108110
$options[] = '--' . $param;
109111
if (is_string($value)) {
110112
$options[] = $value;

cake/tests/cases/console/console_option_parser.test.php

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ConsoleOptionParserTest extends CakeTestCase {
2828
* @return void
2929
*/
3030
function testDescription() {
31-
$parser = new ConsoleOptionParser();
31+
$parser = new ConsoleOptionParser('test', false);
3232
$result = $parser->description('A test');
3333

3434
$this->assertEquals($parser, $result, 'Setting description is not chainable');
@@ -44,7 +44,7 @@ function testDescription() {
4444
* @return void
4545
*/
4646
function testEpilog() {
47-
$parser = new ConsoleOptionParser();
47+
$parser = new ConsoleOptionParser('test', false);
4848
$result = $parser->epilog('A test');
4949

5050
$this->assertEquals($parser, $result, 'Setting epilog is not chainable');
@@ -60,7 +60,7 @@ function testEpilog() {
6060
* @return void
6161
*/
6262
function testAddOptionReturnSelf() {
63-
$parser = new ConsoleOptionParser();
63+
$parser = new ConsoleOptionParser('test', false);
6464
$result = $parser->addOption('test');
6565
$this->assertEquals($parser, $result, 'Did not return $this from addOption');
6666
}
@@ -71,12 +71,12 @@ function testAddOptionReturnSelf() {
7171
* @return void
7272
*/
7373
function testAddOptionLong() {
74-
$parser = new ConsoleOptionParser();
74+
$parser = new ConsoleOptionParser('test', false);
7575
$parser->addOption('test', array(
7676
'short' => 't'
7777
));
7878
$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');
8080
}
8181

8282
/**
@@ -85,12 +85,12 @@ function testAddOptionLong() {
8585
* @return void
8686
*/
8787
function testAddOptionLongEquals() {
88-
$parser = new ConsoleOptionParser();
88+
$parser = new ConsoleOptionParser('test', false);
8989
$parser->addOption('test', array(
9090
'short' => 't'
9191
));
9292
$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');
9494
}
9595

9696
/**
@@ -99,19 +99,19 @@ function testAddOptionLongEquals() {
9999
* @return void
100100
*/
101101
function testAddOptionDefault() {
102-
$parser = new ConsoleOptionParser();
102+
$parser = new ConsoleOptionParser('test', false);
103103
$parser->addOption('test', array(
104104
'default' => 'default value',
105105
));
106106
$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');
108108

109-
$parser = new ConsoleOptionParser();
109+
$parser = new ConsoleOptionParser('test', false);
110110
$parser->addOption('test', array(
111111
'default' => 'default value',
112112
));
113113
$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');
115115
}
116116

117117
/**
@@ -120,12 +120,32 @@ function testAddOptionDefault() {
120120
* @return void
121121
*/
122122
function testAddOptionShort() {
123-
$parser = new ConsoleOptionParser();
123+
$parser = new ConsoleOptionParser('test', false);
124124
$parser->addOption('test', array(
125125
'short' => 't'
126126
));
127127
$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);
129149
}
130150

131151
/**
@@ -134,13 +154,13 @@ function testAddOptionShort() {
134154
* @return void
135155
*/
136156
function testAddOptionMultipleShort() {
137-
$parser = new ConsoleOptionParser();
157+
$parser = new ConsoleOptionParser('test', false);
138158
$parser->addOption('test', array('short' => 't'))
139159
->addOption('file', array('short' => 'f'))
140160
->addOption('output', array('short' => 'o'));
141161

142162
$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);
144164
$this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
145165

146166
$result = $parser->parse(array('-otf'));
@@ -153,13 +173,13 @@ function testAddOptionMultipleShort() {
153173
* @return void
154174
*/
155175
function testMultipleOptions() {
156-
$parser = new ConsoleOptionParser();
176+
$parser = new ConsoleOptionParser('test', false);
157177
$parser->addOption('test')
158178
->addOption('connection')
159179
->addOption('table', array('short' => 't'));
160180

161181
$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);
163183
$this->assertEquals($expected, $result[0], 'multiple options did not parse');
164184
}
165185

@@ -186,12 +206,12 @@ function testAddOptions() {
186206
* @return void
187207
*/
188208
function testOptionWithBooleanParam() {
189-
$parser = new ConsoleOptionParser();
209+
$parser = new ConsoleOptionParser('test', false);
190210
$parser->addOption('no-commit', array('boolean' => true))
191211
->addOption('table', array('short' => 't'));
192212

193213
$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'));
195215
$this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
196216
}
197217

@@ -201,7 +221,7 @@ function testOptionWithBooleanParam() {
201221
* @expectedException InvalidArgumentException
202222
*/
203223
function testOptionThatDoesNotExist() {
204-
$parser = new ConsoleOptionParser();
224+
$parser = new ConsoleOptionParser('test', false);
205225
$parser->addOption('no-commit', array('boolean' => true));
206226

207227
$result = $parser->parse(array('--fail', 'other'));
@@ -214,11 +234,11 @@ function testOptionThatDoesNotExist() {
214234
* @return void
215235
*/
216236
function testOptionWithChoices() {
217-
$parser = new ConsoleOptionParser();
237+
$parser = new ConsoleOptionParser('test', false);
218238
$parser->addOption('name', array('choices' => array('mark', 'jose')));
219239

220240
$result = $parser->parse(array('--name', 'mark'));
221-
$expected = array('name' => 'mark');
241+
$expected = array('name' => 'mark', 'help' => false);
222242
$this->assertEquals($expected, $result[0], 'Got the correct value.');
223243

224244
$result = $parser->parse(array('--name', 'jimmy'));
@@ -230,7 +250,7 @@ function testOptionWithChoices() {
230250
* @return void
231251
*/
232252
function testPositionalArgument() {
233-
$parser = new ConsoleOptionParser();
253+
$parser = new ConsoleOptionParser('test', false);
234254
$result = $parser->addArgument('name', array('help' => 'An argument'));
235255
$this->assertEquals($parser, $result, 'Should returnn this');
236256
}
@@ -241,7 +261,7 @@ function testPositionalArgument() {
241261
* @return void
242262
*/
243263
function testPositionalArgOverwrite() {
244-
$parser = new ConsoleOptionParser();
264+
$parser = new ConsoleOptionParser('test', false);
245265
$parser->addArgument('name', array('help' => 'An argument'))
246266
->addArgument('other', array('index' => 0));
247267

@@ -256,7 +276,7 @@ function testPositionalArgOverwrite() {
256276
* @return void
257277
*/
258278
function testParseArgumentTooMany() {
259-
$parser = new ConsoleOptionParser();
279+
$parser = new ConsoleOptionParser('test', false);
260280
$parser->addArgument('name', array('help' => 'An argument'))
261281
->addArgument('other');
262282

@@ -274,7 +294,7 @@ function testParseArgumentTooMany() {
274294
* @return void
275295
*/
276296
function testPositionalArgNotEnough() {
277-
$parser = new ConsoleOptionParser();
297+
$parser = new ConsoleOptionParser('test', false);
278298
$parser->addArgument('name', array('required' => true))
279299
->addArgument('other', array('required' => true));
280300

@@ -288,7 +308,7 @@ function testPositionalArgNotEnough() {
288308
* @return void
289309
*/
290310
function testPositionalArgWithChoices() {
291-
$parser = new ConsoleOptionParser();
311+
$parser = new ConsoleOptionParser('test', false);
292312
$parser->addArgument('name', array('choices' => array('mark', 'jose')))
293313
->addArgument('alias', array('choices' => array('cowboy', 'samurai')))
294314
->addArgument('weapon', array('choices' => array('gun', 'sword')));
@@ -306,7 +326,7 @@ function testPositionalArgWithChoices() {
306326
* @return void
307327
*/
308328
function testAddArguments() {
309-
$parser = new ConsoleOptionParser();
329+
$parser = new ConsoleOptionParser('test', false);
310330
$result = $parser->addArguments(array(
311331
'name' => array('help' => 'The name'),
312332
'other' => array('help' => 'The other arg')
@@ -323,7 +343,7 @@ function testAddArguments() {
323343
* @return void
324344
*/
325345
function testSubcommand() {
326-
$parser = new ConsoleOptionParser();
346+
$parser = new ConsoleOptionParser('test', false);
327347
$result = $parser->addSubcommand('initdb', array(
328348
'help' => 'Initialize the database'
329349
));
@@ -336,7 +356,7 @@ function testSubcommand() {
336356
* @return void
337357
*/
338358
function testAddSubcommands() {
339-
$parser = new ConsoleOptionParser();
359+
$parser = new ConsoleOptionParser('test', false);
340360
$result = $parser->addSubcommands(array(
341361
'initdb' => array('help' => 'Initialize the database'),
342362
'create' => array('help' => 'Create something')
@@ -589,7 +609,7 @@ function testBuildFromArray() {
589609
* @return void
590610
*/
591611
function testParsingWithSubParser() {
592-
$parser = new ConsoleOptionParser();
612+
$parser = new ConsoleOptionParser('test', false);
593613
$parser->addOption('primary')
594614
->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
595615
->addArgument('two', array('required' => true))
@@ -606,7 +626,12 @@ function testParsingWithSubParser() {
606626
));
607627

608628
$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'));
610635
$this->assertEquals($expected, $result, 'Sub parser did not parse request.');
611636
}
612637
}

0 commit comments

Comments
 (0)