Skip to content

Commit 8f5141d

Browse files
committed
bug symfony#22244 [Console] Fix passing options with defaultCommand (Jakub Sacha)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Fix passing options with defaultCommand | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Seems like overwriting input for the default command is not needed (anymore?). I don't know where the removed comment comes from originally. Use case: i want to call default command and use options at the same time: app/console --abc=true Commits ------- 761de99 Fix passing options with defaultCommand
2 parents ae7e2cd + 761de99 commit 8f5141d

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ public function doRun(InputInterface $input, OutputInterface $output)
189189

190190
if (!$name) {
191191
$name = $this->defaultCommand;
192-
$input = new ArrayInput(array('command' => $this->defaultCommand));
192+
$this->definition->setArguments(array_merge(
193+
$this->definition->getArguments(),
194+
array(
195+
'command' => new InputArgument('command', InputArgument::OPTIONAL, $this->definition->getArgument('command')->getDescription(), $name),
196+
)
197+
));
193198
}
194199

195200
$this->runningCommand = null;

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static function setUpBeforeClass()
3939
{
4040
self::$fixturesPath = realpath(__DIR__.'/Fixtures/');
4141
require_once self::$fixturesPath.'/FooCommand.php';
42+
require_once self::$fixturesPath.'/FooOptCommand.php';
4243
require_once self::$fixturesPath.'/Foo1Command.php';
4344
require_once self::$fixturesPath.'/Foo2Command.php';
4445
require_once self::$fixturesPath.'/Foo3Command.php';
@@ -1116,16 +1117,31 @@ public function testSetRunCustomDefaultCommand()
11161117
$application->setDefaultCommand($command->getName());
11171118

11181119
$tester = new ApplicationTester($application);
1119-
$tester->run(array());
1120-
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
1120+
$tester->run(array(), array('interactive' => false));
1121+
$this->assertEquals('called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
11211122

11221123
$application = new CustomDefaultCommandApplication();
11231124
$application->setAutoExit(false);
11241125

11251126
$tester = new ApplicationTester($application);
1126-
$tester->run(array());
1127+
$tester->run(array(), array('interactive' => false));
11271128

1128-
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
1129+
$this->assertEquals('called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
1130+
}
1131+
1132+
public function testSetRunCustomDefaultCommandWithOption()
1133+
{
1134+
$command = new \FooOptCommand();
1135+
1136+
$application = new Application();
1137+
$application->setAutoExit(false);
1138+
$application->add($command);
1139+
$application->setDefaultCommand($command->getName());
1140+
1141+
$tester = new ApplicationTester($application);
1142+
$tester->run(array('--fooopt' => 'opt'), array('interactive' => false));
1143+
1144+
$this->assertEquals('called'.PHP_EOL.'opt'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
11291145
}
11301146

11311147
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Input\InputOption;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class FooOptCommand extends Command
9+
{
10+
public $input;
11+
public $output;
12+
13+
protected function configure()
14+
{
15+
$this
16+
->setName('foo:bar')
17+
->setDescription('The foo:bar command')
18+
->setAliases(array('afoobar'))
19+
->addOption('fooopt', 'fo', InputOption::VALUE_OPTIONAL, 'fooopt description')
20+
;
21+
}
22+
23+
protected function interact(InputInterface $input, OutputInterface $output)
24+
{
25+
$output->writeln('interact called');
26+
}
27+
28+
protected function execute(InputInterface $input, OutputInterface $output)
29+
{
30+
$this->input = $input;
31+
$this->output = $output;
32+
33+
$output->writeln('called');
34+
$output->writeln($this->input->getOption('fooopt'));
35+
}
36+
}

src/Symfony/Component/Console/Tests/Fixtures/application_run2.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Usage:
22
help [options] [--] [<command_name>]
33

44
Arguments:
5-
command The command to execute
5+
command The command to execute [default: "list"]
66
command_name The command name [default: "help"]
77

88
Options:

0 commit comments

Comments
 (0)