8000 feature #16906 [Console] Better support for one command app (lyrixx) · symfony/symfony@1ab32c5 · GitHub
[go: up one dir, main page]

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

Commit 1ab32c5

Browse files
committed
feature #16906 [Console] Better support for one command app (lyrixx)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Console] Better support for one command app | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #9564 | License | MIT Hello; I write many CLI application, and "single command" in cli application is not so easy to write. This is why I propose this patch. IMHO, this PR could replaces #9609. See it in application: ```php #!/usr/bin/env php <?php require __DIR__.'/vendor/autoload.php'; use Symfony\Component\Console\Application; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; (new Application('echo', '1.0.0')) ->register('echo') ->addArgument('foo', InputArgument::OPTIONAL, 'The directory', 'foo') ->addOption('bar', null, InputOption::VALUE_REQUIRED, 'Foobar', 'bar') ->setCode(function(InputInterface $input, OutputInterface $output) { $output->writeln('start'); $output->writeln($input->getArgument('foo')); $output->writeln($input->getOption('bar')); }) ->getApplication() ->setSingleCommand('echo') ->run(); ``` Some usage: ``` >(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php start foo bar ``` ``` >(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php "first argument" start first argument bar ``` ``` >(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php "first argument" --bar="first option" start first argument first option ``` ``` >(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php "first argument" --bar="first option" --help Usage: echo [options] [--] [<foo>] Arguments: foo The directory [default: "foo"] Options: --bar=BAR Foobar [default: "bar"] -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 ``` Commits ------- 4a9bb1d [Console] Better support for one command app
2 parents ba34459 + 4a9bb1d commit 1ab32c5

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class Application
6666
private $dispatcher;
6767
private $terminalDimensions;
6868
private $defaultCommand;
69+
private $singleCommand;
6970

7071
/**
7172
* Constructor.
@@ -168,7 +169,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
168169
if (true === $input->hasParameterOption(array('--help', '-h'), true)) {
169170
if (!$name) {
170171
$name = 'help';
171-
$input = new ArrayInput(array('command' => 'help'));
172+
$input = new ArrayInput(array('command_name' => $this->defaultCommand));
172173
} else {
173174
$this->wantHelps = true;
174175
}
@@ -226,6 +227,13 @@ public function setDefinition(InputDefinition $definition)
226227
*/
227228
public function getDefinition()
228229
{
230+
if ($this->singleCommand) {
231+
$inputDefinition = $this->definition;
232+
$inputDefinition->setArguments();
233+
234+
return $inputDefinition;
235+
}
236+
229237
return $this->definition;
230238
}
231239

@@ -859,7 +867,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
859867
*/
860868
protected function getCommandName(InputInterface $input)
861869
{
862-
return $input->getFirstArgument();
870+
return $this->singleCommand ? $this->defaultCommand : $input->getFirstArgument();
863871
}
864872

865873
/**
@@ -1039,11 +1047,21 @@ private function findAlternatives($name, $collection)
10391047
/**
10401048
* Sets the default Command name.
10411049
*
1042-
* @param string $commandName The Command name
1050+
* @param string $commandName The Command name
1051+
* @param bool $isSingleCommand Set to true if there is only one command in this application
10431052
*/
1044-
public function setDefaultCommand($commandName)
1053+
public function setDefaultCommand($commandName, $isSingleCommand = false)
10451054
{
10461055
$this->defaultCommand = $commandName;
1056< 8000 span class="diff-text-marker">+
1057+
if ($isSingleCommand) {
1058+
// Ensure the command exist
1059+
$this->find($commandName);
1060+
1061+
$this->singleCommand = true;
1062+
}
1063+
1064+
return $this;
10471065
}
10481066

10491067
private function stringWidth($string)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,24 @@ public function testSetRunCustomDefaultCommand()
10861086
$this->assertEquals('interact called'.PHP_EOL.'called'.PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');
10871087
}
10881088

1089+
public function testSetRunCustomSingleCommand()
1090+
{
1091+
$command = new \FooCommand();
1092+
1093+
$application = new Application();
1094+
$application->setAutoExit(false);
1095+
$application->add($command);
1096+
$application->setDefaultCommand($command->getName(), true);
1097+
1098+
$tester = new ApplicationTester($application);
1099+
1100+
$tester->run(array());
1101+
$this->assertContains('called', $tester->getDisplay());
1102+
1103+
$tester->run(array('--help' => true));
1104+
$this->assertContains('The foo:bar command', $tester->getDisplay());
1105+
}
1106+
10891107
/**
10901108
* @requires function posix_isatty
10911109
*/
Lines changed: 15 additions & 17 deletions
67E6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
Usage:
2-
help [options] [--] [<command_name>]
2+
list [options] [--] [<namespace>]
33

44
Arguments:
5-
command The command to execute
6-
command_name The command name [default: "help"]
5+
namespace The namespace name
76

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

1911
Help:
20-
The help command displays help for a given command:
12+
The list command lists all commands:
2113

22-
php app/console help list
14+
php app/console list
2315

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

26-
php app/console help --format=xml list
18+
php app/console list test
2719

28-
To display the list of available commands, please use the list command.
20+
You can also output the information in other formats by using the --format option:
21+
22+
php app/console list --format=xml
23+
24+
It's also possible to get raw list of commands (useful for embedding command runner):
25+
26+
php app/console list --raw

0 commit comments

Comments
 (0)
0