Skip to content

[Console] Better support for one command app #16906

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
Jun 15, 2016
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
26 changes: 22 additions & 4 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Application
private $dispatcher;
private $terminalDimensions;
private $defaultCommand;
private $singleCommand;

/**
* Constructor.
Expand Down Expand Up @@ -168,7 +169,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
if (true === $input->hasParameterOption(array('--help', '-h'), true)) {
if (!$name) {
$name = 'help';
$input = new ArrayInput(array('command' => 'help'));
$input = new ArrayInput(array('command_name' => $this->defaultCommand));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be in 2.3 isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, indeed, this is a bug fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you change this. the argument is named command, not command_name in the Application

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {
$this->wantHelps = true;
}
Expand Down Expand Up @@ -226,6 +227,13 @@ public function setDefinition(InputDefinition $definition)
*/
public function getDefinition()
{
if ($this->singleCommand) {
$inputDefinition = $this->definition;
$inputDefinition->setArguments();

return $inputDefinition;
}

return $this->definition;
}

Expand Down Expand Up @@ -859,7 +867,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
*/
protected function getCommandName(InputInterface $input)
{
return $input->getFirstArgument();
return $this->singleCommand ? $this->defaultCommand : $input->getFirstArgument();
}

/**
Expand Down Expand Up @@ -1039,11 +1047,21 @@ private function findAlternatives($name, $collection)
/**
* Sets the default Command name.
*
* @param string $commandName The Command name
* @param string $commandName The Command name
* @param bool $isSingleCommand Set to true if there is only one command in this application
*/
public function setDefaultCommand($commandName)
public function setDefaultCommand($commandName, $isSingleCommand = false)
{
$this->defaultCommand = $commandName;

if ($isSingleCommand) {
// Ensure the command exist
$this->find($commandName);

$this->singleCommand = true;
}

return $this;
}

private function stringWidth($string)
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,24 @@ public function testSetRunCustomDefaultCommand()
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
}

public function testSetRunCustomSingleCommand()
{
$command = new \FooCommand();

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

$tester = new ApplicationTester($application);

$tester->run(array());
$this->assertContains('called', $tester->getDisplay());

$tester->run(array('--help' => true));
$this->assertContains('The foo:bar command', $tester->getDisplay());
}

/**
* @requires function posix_isatty
*/
Expand Down
32 changes: 15 additions & 17 deletions src/Symfony/Component/Console/Tests/Fixtures/application_run2.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
Usage:
help [options] [--] [<command_name>]
list [options] [--] [<namespace>]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the fixture, because now, the --help display the help of the default command.


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

Options:
--format=FORMAT The output format (txt, xml, json, or md) [default: "txt"]
--raw To output raw command help
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
--raw To output raw command list
--format=FORMAT The output format (txt, xml, json, or md) [default: "txt"]

Help:
The help command displays help for a given command:
The list command lists all commands:

php app/console help list
php app/console list

You can also output the help in other formats by using the --format option:
You can also display the commands for a specific namespace:

php app/console help --format=xml list
php app/console list test

To display the list of available commands, please use the list command.
You can also output the information in other formats by using the --format option:

php app/console list --format=xml

It's also possible to get raw list of commands (useful for embedding command runner):

php app/console list --raw