Browse Source

Don't duplicate command names in help listing.

Only show the shortest name that resolves to an individual classname.
This simplifies the help output and makes it similar to what we had in
the ShellDispatcher implementation.

Refs #11362
Mark Story 8 years ago
parent
commit
db457a4ec2
2 changed files with 30 additions and 37 deletions
  1. 24 36
      src/Command/HelpCommand.php
  2. 6 1
      tests/TestCase/Command/HelpCommandTest.php

+ 24 - 36
src/Command/HelpCommand.php

@@ -21,7 +21,6 @@ use Cake\Console\CommandCollectionAwareInterface;
 use Cake\Console\ConsoleIo;
 use Cake\Console\ConsoleOptionParser;
 use Cake\Console\ConsoleOutput;
-use Cake\Shell\Task\CommandTask;
 use Cake\Utility\Inflector;
 use SimpleXmlElement;
 
@@ -64,13 +63,8 @@ class HelpCommand extends Command implements CommandCollectionAwareInterface
             $io->out('<info>Available Commands:</info>', 2);
         }
 
-        if (!$this->commands) {
-            $this->commands = new CommandCollection($this->getCommands($io));
-        }
-
         $commands = $this->commands->getIterator();
         $commands->ksort();
-        $commands = new CommandCollection((array)$commands);
 
         if ($args->getOption('xml')) {
             $this->asXml($io, $commands);
@@ -83,43 +77,37 @@ class HelpCommand extends Command implements CommandCollectionAwareInterface
     }
 
     /**
-     * Get the list of commands using the CommandTask
-     *
-     * Provides backwards compatibility when an application doesn't use
-     * CommandRunner.
-     *
-     * @param \Cake\Console\ConsoleIo $io The console io
-     * @return array
-     */
-    protected function getCommands($io)
-    {
-        $task = new CommandTask($io);
-        $nested = $task->getShellList();
-        $out = [];
-        foreach ($nested as $section => $commands) {
-            $prefix = '';
-            if ($section !== 'CORE' && $section !== 'app') {
-                $prefix = Inflector::underscore($section) . '.';
-            }
-            foreach ($commands as $command) {
-                $out[$prefix . $command] = $command;
-            }
-        }
-
-        return $out;
-    }
-
-    /**
      * Output text.
      *
      * @param \Cake\Console\ConsoleIo $io The console io
-     * @param \Cake\Console\CommandCollection $commands The command collection to output.
+     * @param \ArrayIterator $commands The command collection to output.
      * @return void
      */
     protected function asText($io, $commands)
     {
+        $invert = [];
         foreach ($commands as $name => $class) {
-            $io->out('- ' . $name);
+            if (!isset($invert[$class])) {
+                $invert[$class] = [];
+            }
+            $invert[$class][] = $name;
+        }
+
+        foreach ($commands as $name => $class) {
+            if (count($invert[$class]) == 1) {
+                $io->out('- ' . $name);
+            }
+
+            if (count($invert[$class]) > 1) {
+                // Sort by length so we can get the shortest name.
+                usort($invert[$class], function ($a, $b) {
+                    return strlen($a) - strlen($b);
+                });
+                $io->out('- ' . array_shift($invert[$class]));
+
+                // Empty the list to prevent duplicates
+                $invert[$class] = [];
+            }
         }
         $io->out('');
 
@@ -131,7 +119,7 @@ class HelpCommand extends Command implements CommandCollectionAwareInterface
      * Output as XML
      *
      * @param \Cake\Console\ConsoleIo $io The console io
-     * @param \Cake\Console\CommandCollection $commands The command collection to output
+     * @param \ArrayIterator $commands The command collection to output
      * @return void
      */
     protected function asXml($io, $commands)

+ 6 - 1
tests/TestCase/Command/HelpCommandTest.php

@@ -68,10 +68,15 @@ class HelpShellTest extends ConsoleIntegrationTestCase
      */
     protected function assertCommandList()
     {
+        $this->assertOutputContains('- widget', 'plugin command');
+        $this->assertOutputNotContains(
+            '- test_plugin.widget',
+            'only short alias for plugin command.'
+        );
         $this->assertOutputContains('- sample', 'app shell');
         $this->assertOutputContains('- test_plugin.sample', 'Long plugin name');
         $this->assertOutputContains('- routes', 'core shell');
-        $this->assertOutputContains('- test_plugin.example', 'Long plugin name');
+        $this->assertOutputContains('- example', 'short plugin name');
         $this->assertOutputContains('To run a command', 'more info present');
         $this->assertOutputContains('To get help', 'more info present');
     }