8000 feature #23869 [Console] Made console command shortcuts case insensit… · symfony/symfony@c82ec96 · GitHub
[go: up one dir, main page]

Skip to content

Commit c82ec96

Browse files
author
Robin Chalas
committed
feature #23869 [Console] Made console command shortcuts case insensitive (thanosp)
This PR was merged into the 3.4 branch. Discussion ---------- [Console] Made console command shortcuts case insensitive | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!--highly recommended for new features--> <!-- - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the 3.4, legacy code removals go to the master branch. - Please fill in this template according to the PR you're about to submit. - Replace this comment by a description of what your PR is solving. --> This patch would save a lot of time wasted correcting typos. Symfony commands are using `:` as a namespace separator. Most keyboards require pressing shift to get this character. As an accident a developer in hurry (me) when trying to use the shortcut of commands will often type the character after `:` also while pressing shift. Right now this will lead to an error effectively wasting the attempt to save time by using the shortcut. e.g. `bin/console c:C` Commits ------- 04df283 [Console] Added a case-insensitive fallback for console command names
2 parents eeaea83 + 04df283 commit c82ec96

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,12 @@ public function find($name)
578578
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
579579
$commands = preg_grep('{^'.$expr.'}', $allCommands);
580580

581-
if (empty($commands) || count(preg_grep('{^'.$expr.'$}', $commands)) < 1) {
581+
if (empty 8000 ($commands)) {
582+
$commands = preg_grep('{^'.$expr.'}i', $allCommands);
583+
}
584+
585+
// if no commands matched or we just matched namespaces
586+
if (empty($commands) || count(preg_grep('{^'.$expr.'$}i', $commands)) < 1) {
582587
if (false !== $pos = strrpos($name, ':')) {
583588
// check if a namespace exists and contains commands
584589
$this->findNamespace(substr($name, 0, $pos));

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added `CommandLoaderInterface`, `FactoryCommandLoader` and PSR-11
88
`ContainerCommandLoader` for commands lazy-loading
9+
* added a case-insensitive command name matching fallback
910

1011
3.3.0
1112
-----

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public static function setUpBeforeClass()
5151
require_once self::$fixturesPath.'/Foo3Command.php';
5252
require_once self::$fixturesPath.'/Foo4Command.php';
5353
require_once self::$fixturesPath.'/Foo5Command.php';
54+
require_once self::$fixturesPath.'/FooSameCaseUppercaseCommand.php';
55+
require_once self::$fixturesPath.'/FooSameCaseLowercaseCommand.php';
5456
require_once self::$fixturesPath.'/FoobarCommand.php';
5557
require_once self::$fixturesPath.'/BarBucCommand.php';
5658
require_once self::$fixturesPath.'/FooSubnamespaced1Command.php';
@@ -317,6 +319,41 @@ public function testFind()
317319
$this->assertInstanceOf('FooCommand', $application->find('a'), '->find() returns a command if the abbreviation exists for an alias');
318320
}
319321

322+
public function testFindCaseSensitiveFirst()
323+
{
324+
$application = new Application();
325+
$application->add(new \FooSameCaseUppercaseCommand());
326+
$application->add(new \FooSameCaseLowercaseCommand());
327+
328+
$this->assertInstanceOf('FooSameCaseUppercaseCommand', $application->find('f:B'), '->find() returns a command if the abbreviation is the correct case');
329+
$this->assertInstanceOf('FooSameCaseUppercaseCommand', $application->find('f:BAR'), '->find() returns a command if the abbreviation is the correct case');
330+
$this->assertInstanceOf('FooSameCaseLowercaseCommand', $application->find('f:b'), '->find() returns a command if the abbreviation is the correct case');
331+
$this->assertInstanceOf('FooSameCaseLowercaseCommand', $application->find('f:bar'), '->find() returns a command if the abbreviation is the correct case');
332+
}
333+
334+
public function testFindCaseInsensitiveAsFallback()
335+
{
336+
$application = new Application();
337+
$application->add(new \FooSameCaseLowercaseCommand());
338+
339+
$this->assertInstanceOf('FooSameCaseLowercaseCommand', $application->find('f:b'), '->find() returns a command if the abbreviation is the correct case');
340+
$this->assertInstanceOf('FooSameCaseLowercaseCommand', $application->find('f:B'), '->find() will fallback to case insensitivity');
341+
$this->assertInstanceOf('FooSameCaseLowercaseCommand', $application->find('FoO:BaR'), '->find() will fallback to case insensitivity');
342+
}
343+
344+
/**
345+
* @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
346+
* @expectedExceptionMessage Command "FoO:BaR" is ambiguous
347+
*/
348+
public function testFindCaseInsensitiveSuggestions()
349+
{
350+
$application = new Application();
351+
$application->add(new \FooSameCaseLowercaseCommand());
352+
$application->add(new \FooSameCaseUppercaseCommand());
353+
354+
$this->assertInstanceOf('FooSameCaseLowercaseCommand', $application->find('FoO:BaR'), '->find() will find two suggestions with case insensitivity');
355+
}
356+
320357
public function testFindWithCommandLoader()
321358
{
322359
$application = new Application();
@@ -414,8 +451,8 @@ public function testFindAlternativeExceptionMessageSingle($name)
414451
public function provideInvalidCommandNamesSingle()
415452
{
416453
return array(
417-
array('foo3:baR'),
418-
array('foO3:bar'),
454+
array('foo3:barr'),
455+
array('fooo3:bar'),
419456
);
420457
}
421458

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
5+
class FooSameCaseLowercaseCommand extends Command
6+
{
7+
protected function configure()
8+
{
9+
$this->setName('foo:bar')->setDescription('foo:bar command');
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
5+
class FooSameCaseUppercaseCommand extends Command
6+
{
7+
protected function configure()
8+
{
9+
$this->setName('foo:BAR')->setDescription('foo:BAR command');
10+
}
11+
}

0 commit comments

Comments
 (0)
0