@@ -50,18 +50,29 @@ public function run(CommandParser $commandParser, OutputInterface $output): int
50
50
return self ::CLI_SUCCESS ;
51
51
}
52
52
53
- $ command = null ;
53
+ $ commands = [] ;
54
54
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 ;
58
57
}
59
58
}
60
59
61
- if ($ command === null ) {
60
+ if (empty ( $ commands ) ) {
62
61
throw new InvalidArgumentException (sprintf ('Command "%s" is not defined. ' , $ commandParser ->getCommandName ()));
63
62
}
64
63
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 ];
65
76
if ($ commandParser ->hasOption ('help ' )) {
66
77
$ this ->showCommandHelp ($ command , $ output );
67
78
return self ::CLI_SUCCESS ;
@@ -152,4 +163,9 @@ private function showCommandHelp(CommandInterface $selectedCommand, OutputInterf
152
163
}
153
164
$ consoleOutput ->listKeyValues ($ options , true );
154
165
}
166
+
167
+ private static function stringStartsWith (string $ haystack , string $ needle ): bool
168
+ {
169
+ return substr ($ haystack , 0 , strlen ($ needle )) === $ needle ;
170
+ }
155
171
}
0 commit comments