Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 672786f

Browse filesBrowse files
committed
[Console] Explicitly passed options without value (or empty) should remain empty
1 parent 44f6a8c commit 672786f
Copy full SHA for 672786f

File tree

Expand file treeCollapse file tree

7 files changed

+50
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

7 files changed

+50
-6
lines changed
Open diff view settings
Collapse file

‎src/Symfony/Component/Console/Input/ArgvInput.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/ArgvInput.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ private function addLongOption($name, $value)
246246
throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
247247
}
248248

249-
if (!$option->isArray()) {
250-
$value = $option->isValueOptional() ? $option->getDefault() : true;
249+
if (!$option->isArray() && !$option->isValueOptional()) {
250+
$value = true;
251251
}
252252
}
253253

Collapse file

‎src/Symfony/Component/Console/Input/ArrayInput.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/ArrayInput.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ private function addLongOption($name, $value)
165165
throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name));
166166
}
167167

168-
$value = $option->isValueOptional() ? $option->getDefault() : true;
168+
if (!$option->isValueOptional()) {
169+
$value = true;
170+
}
169171
}
170172

171173
$this->options[$name] = $value;
Collapse file

‎src/Symfony/Component/Console/Input/Input.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Input/Input.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function getOption($name)
154154
throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
155155
}
156156

157-
return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
157+
return array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
158158
}
159159

160160
/**
Collapse file

‎src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public function provideOptions()
8383
array('foo' => null),
8484
'->parse() parses long options with optional value which is empty (with a = separator) followed by an argument',
8585
),
86+
array(
87+
array('cli.php', '--foo'),
88+
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
89+
array('foo' => null),
90+
'->parse() parses long options with optional value which is empty as null',
91+
),
8692
array(
8793
array('cli.php', '-f'),
8894
array(new InputOption('foo', 'f')),
Collapse file

‎src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ public function provideOptions()
8080
'->parse() parses long options with a default value',
8181
),
8282
array(
83-
array('--foo' => null),
83+
array(),
8484
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
8585
array('foo' => 'default'),
86-
'->parse() parses long options with a default value',
86+
'->parse() use the default value for long options which are not passed',
87+
),
88+
array(
89+
array('--foo' => null),
90+
array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
91+
array('foo' => null),
92+
'->parse() keeps empty values for long options with default values',
8793
),
8894
array(
8995
array('-f' => 'bar'),
Collapse file

‎src/Symfony/Component/Console/Tests/Input/InputTest.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Input/InputTest.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public function testOptions()
3636
$input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
3737
$this->assertEquals('default', $input->getOption('bar'), '->getOption() returns the default value for optional options');
3838
$this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getOptions(), '->getOptions() returns all option values, even optional ones');
39+
40+
$input = new ArrayInput(array('--name' => 'foo', '--bar' => ''), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
41+
$this->assertEquals('', $input->getOption('bar'), '->getOption() returns null for options explicitly passed without value (or an empty value)');
42+
$this->assertEquals(array('name' => 'foo', 'bar' => ''), $input->getOptions(), '->getOptions() returns all option values.');
43+
44+
$input = new ArrayInput(array('--name' => 'foo', '--bar' => null), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
45+
$this->assertNull($input->getOption('bar'), '->getOption() returns null for options explicitly passed without value (or an empty value)');
46+
$this->assertEquals(array('name' => 'foo', 'bar' => null), $input->getOptions(), '->getOptions() returns all option values');
3947
}
4048

4149
/**
Collapse file

‎test.php‎

Copy file name to clipboard
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use Symfony\Component\Console\Application;
6+
use Symfony\Component\Console\Input\InputOption;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
9+
$application = new Application();
10+
$application
11+
->register('echo')
12+
->addOption('prefix', null, InputOption::VALUE_OPTIONAL, null, 'my-default')
13+
->addOption('call', null, InputOption::VALUE_OPTIONAL, null, 'call-default')
14+
->addArgument('value', InputArgument::REQUIRED)
15+
->setCode(function ($input, $output) {
16+
var_dump($input->getOption('prefix'));
17+
var_dump($input->getArgument('value'));
18+
var_dump($input->getOption('call'));
19+
});
20+
21+
$application->run();
22+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.