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

[Console] Fix passing options with defaultCommand #22244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion 7 src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ public function doRun(InputInterface $input, OutputInterface $output)

if (!$name) {
$name = $this->defaultCommand;
$input = new ArrayInput(array('command' => $this->defaultCommand));
$this->definition->setArguments(array_merge(
$this->definition->getArguments(),
array(
'command' => new InputArgument('command', InputArgument::OPTIONAL, $this->definition->getArgument('command')->getDescription(), $name),
)
));
}

// the command name MUST be the first element of the input
Expand Down
24 changes: 20 additions & 4 deletions 24 src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static function setUpBeforeClass()
{
self::$fixturesPath = realpath(__DIR__.'/Fixtures/');
require_once self::$fixturesPath.'/FooCommand.php';
require_once self::$fixturesPath.'/FooOptCommand.php';
require_once self::$fixturesPath.'/Foo1Command.php';
require_once self::$fixturesPath.'/Foo2Command.php';
require_once self::$fixturesPath.'/Foo3Command.php';
Expand Down Expand Up @@ -1123,16 +1124,31 @@ public function testSetRunCustomDefaultCommand()
$application->setDefaultCommand($command->getName());

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

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

$tester = new ApplicationTester($application);
$tester->run(array());
$tester->run(array(), array('interactive' => false));

$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
$this->assertEquals('called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
}

public function testSetRunCustomDefaultCommandWithOption()
{
$command = new \FooOptCommand();

$application = new Application();
$application->setAutoExit(false);
$application->add($command);
$application->setDefaultCommand($command->getName());

$tester = new ApplicationTester($application);
$tester->run(array('--fooopt' => 'opt'), array('interactive' => false));

$this->assertEquals('called'.PHP_EOL.'opt'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
}

/**
Expand Down
36 changes: 36 additions & 0 deletions 36 src/Symfony/Component/Console/Tests/Fixtures/FooOptCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class FooOptCommand extends Command
{
public $input;
public $output;

protected function configure()
{
$this
->setName('foo:bar')
->setDescription('The foo:bar command')
->setAliases(array('afoobar'))
->addOption('fooopt', 'fo', InputOption::VALUE_OPTIONAL, 'fooopt description')
;
}

protected function interact(InputInterface $input, OutputInterface $output)
{
$output->writeln('interact called');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;

$output->writeln('called');
$output->writeln($this->input->getOption('fooopt'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Usage:
help [options] [--] [<command_name>]

Arguments:
command The command to execute
command The command to execute [default: "list"]
command_name The command name [default: "help"]

Options:
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.