Skip to content

Commit

Permalink
Properly handle VALUE_NONE options
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Kmieliauskas committed Jan 15, 2019
1 parent a1c7ac6 commit c04f4a9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
17 changes: 13 additions & 4 deletions src/Console/Input/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@

final class Option
{
/**
* @param InputInterface $input
* @param InputOption $option
*
* @return string
*/
public static function toString(
InputInterface $input,
InputOption $option
): string {
$name = $option->getName();

if (!$option->acceptValue()) {
return \sprintf(
'--%s',
$name
);
return true === $input->getOption($name)
? \sprintf('--%s', $name)
: '';
}

if (!$option->isArray()) {
Expand Down Expand Up @@ -53,7 +58,11 @@ public static function toString(
}

/**
* @param InputOption $option
* @param string $name
* @param null|string $value
*
* @return string
*/
private static function generatePartialOption(
InputOption $option,
Expand Down
5 changes: 3 additions & 2 deletions src/Executor/ParallelExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Deployer\Task\Context;
use Deployer\Task\Task;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -269,7 +268,9 @@ private function generateOptions(): string
$inputs[] = Option::toString($this->input, $option);
}

return implode(' ', $inputs);
return implode(' ', array_filter($inputs, function (?string $item) {
return $item !== '';
}));
}

private function generateArguments(): string
Expand Down
15 changes: 9 additions & 6 deletions test/src/Console/Input/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ public function toStringProvider(): \Generator
{
// InputOption::VALUE_NONE
foreach ([
['--fooBar', 'fooBar'],
['--0', '0'],
['--1', '1'],
['--foo\-%&Bar', 'foo\-%&Bar'],
['--ù+ì', 'ù+ì'],
] as list($expectedValue, $optionName)) {
['--fooBar', 'fooBar', true],
['--0', '0', true],
['--1', '1', true],
['--foo\-%&Bar', 'foo\-%&Bar', true],
['--ù+ì', 'ù+ì', true],
] as list($expectedValue, $optionName, $optionValue)) {
$input = $this->createMock(InputInterface::class);
$input->expects($this->once())
->method('getOption')
->willReturn($optionValue);

$option = $this->createMock(InputOption::class);
$option->expects($this->once())
Expand Down

0 comments on commit c04f4a9

Please sign in to comment.