Skip to content

Commit 761de99

Browse files
committed
Fix passing options with defaultCommand
1 parent 2adfb37 commit 761de99

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
@@ -179,7 +179,12 @@ public function doRun(InputInterface $input, OutputInterface $output)
179179

180180
if (!$name) {
181181
$name = $this->defaultCommand;
182-
$input = new ArrayInput(array('command' => $this->defaultCommand));
182+
$this->definition->setArguments(array_merge(
183+
$this->definition->getArguments(),
184+
array(
185+
'command' => new InputArgument('command', InputArgument::OPTIONAL, $this->definition->getArgument('command')->getDescription(), $name),
186+
)
187+
));
183188
}
184189

185190
// the command name MUST be the first element of the input

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';
@@ -1123,16 +1124,31 @@ public function testSetRunCustomDefaultCommand()
11231124
$application->setDefaultCommand($command->getName());
11241125

11251126
$tester = new ApplicationTester($application);
1126-
$tester->run(array());
1127-
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
1127+
$tester->run(array(), array('interactive' => false));
1128+
$this->assertEquals('called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
11281129

11291130
$application = new CustomDefaultCommandApplication();
11301131
$application->setAutoExit(false);
11311132

11321133
$tester = new ApplicationTester($application);
1133-
$tester->run(array());
1134+
$tester->run(array(), array('interactive' => false));
11341135

1135-
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
1136+
$this->assertEquals('called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
1137+
}
1138+
1139+
public function testSetRunCustomDefaultCommandWithOption()
1140+
{
1141+
$command = new \FooOptCommand();
1142+
1143+
$application = new Application();
1144+
$application->setAutoExit(false);
1145+
$application->add($command);
1146+
$application->setDefaultCommand($command->getName());
1147+
1148+
$tester = new ApplicationTester($application);
1149+
$tester->run(array('--fooopt' => 'opt'), array('interactive' => false));
1150+
1151+
$this->assertEquals('called'.PHP_EOL.'opt'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
11361152
}
11371153

11381154
/**
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)