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

Sign up
Appearance settings

Commit 6bac55b

Browse files
can execute any command with prefix
1 parent 106fc5a commit 6bac55b

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

‎src/CommandRunner.php‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,29 @@ public function run(CommandParser $commandParser, OutputInterface $output): int
5050
return self::CLI_SUCCESS;
5151
}
5252

53-
$command = null;
53+
$commands = [];
5454
foreach ($this->commands as $currentCommand) {
55-
if ($currentCommand->getName() === $commandParser->getCommandName()) {
56-
$command = $currentCommand;
57-
break;
55+
if (self::stringStartsWith($currentCommand->getName(), $commandParser->getCommandName())) {
56+
$commands[] = $currentCommand;
5857
}
5958
}
6059

61-
if ($command === null) {
60+
if (empty($commands)) {
6261
throw new InvalidArgumentException(sprintf('Command "%s" is not defined.', $commandParser->getCommandName()));
6362
}
6463

64+
if (count($commands) > 1) {
65+
$names = [];
66+
foreach ($commands as $command) {
67+
$names[$command->getName()] = $command->getDescription();
68+
}
69+
$consoleOutput = new ConsoleOutput($output);
70+
$consoleOutput->error(sprintf('Command "%s" is ambiguous.', $commandParser->getCommandName()));
71+
$consoleOutput->listKeyValues($names, true);
72+
return self::CLI_ERROR;
73+
}
74+
75+
$command = $commands[0];
6576
if ($commandParser->hasOption('help')) {
6677
$this->showCommandHelp($command, $output);
6778
return self::CLI_SUCCESS;
@@ -152,4 +163,9 @@ private function showCommandHelp(CommandInterface $selectedCommand, OutputInterf
152163
}
153164
$consoleOutput->listKeyValues($options, true);
154165
}
166+
167+
private static function stringStartsWith(string $haystack, string $needle): bool
168+
{
169+
return substr($haystack, 0, strlen($needle)) === $needle;
170+
}
155171
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /