Browse Source

added ability to set a command description (#16366)

Added ability to set a description inside commands

Co-authored-by: Kevin Pfeifer <kevin.pfeifer@sunlime.at>
Co-authored-by: ADmad <ADmad@users.noreply.github.com>
Co-authored-by: Mark Story <mark@mark-story.com>
Kevin Pfeifer 4 years ago
parent
commit
0301e94bd2

+ 11 - 0
src/Console/BaseCommand.php

@@ -66,6 +66,16 @@ abstract class BaseCommand implements CommandInterface
     }
 
     /**
+     * Get the command description.
+     *
+     * @return string
+     */
+    public static function getDescription(): string
+    {
+        return '';
+    }
+
+    /**
      * Get the root command name.
      *
      * @return string
@@ -109,6 +119,7 @@ abstract class BaseCommand implements CommandInterface
         [$root, $name] = explode(' ', $this->name, 2);
         $parser = new ConsoleOptionParser($name);
         $parser->setRootName($root);
+        $parser->setDescription(static::getDescription());
 
         $parser = $this->buildOptionParser($parser);
         if ($parser->subcommands()) {

+ 9 - 3
src/Console/Command/HelpCommand.php

@@ -112,7 +112,10 @@ class HelpCommand extends BaseCommand implements CommandCollectionAwareInterface
                 [, $shortestName] = explode('.', $shortestName, 2);
             }
 
-            $grouped[$prefix][] = $shortestName;
+            $grouped[$prefix][] = [
+                'name' => $shortestName,
+                'description' => is_subclass_of($class, BaseCommand::class) ? $class::getDescription() : '',
+            ];
         }
         ksort($grouped);
 
@@ -122,8 +125,11 @@ class HelpCommand extends BaseCommand implements CommandCollectionAwareInterface
         foreach ($grouped as $prefix => $names) {
             $io->out("<info>{$prefix}</info>:");
             sort($names);
-            foreach ($names as $name) {
-                $io->out(' - ' . $name);
+            foreach ($names as $data) {
+                $io->out(' - ' . $data['name']);
+                if ($data['description']) {
+                    $io->info(str_pad(" \u{2514}", 13, "\u{2500}") . ' ' . $data['description']);
+                }
             }
             $io->out('');
         }

+ 1 - 0
tests/TestCase/Console/Command/HelpCommandTest.php

@@ -95,6 +95,7 @@ class HelpCommandTest extends TestCase
         $this->assertOutputContains('- abort', 'command object');
         $this->assertOutputContains('To run a command', 'more info present');
         $this->assertOutputContains('To get help', 'more info present');
+        $this->assertOutputContains('This is a demo command', 'command description missing');
     }
 
     /**

+ 5 - 0
tests/test_app/TestApp/Command/DemoCommand.php

@@ -9,6 +9,11 @@ use Cake\Console\ConsoleIo;
 
 class DemoCommand extends Command
 {
+    public static function getDescription(): string
+    {
+        return 'This is a demo command';
+    }
+
     public function execute(Arguments $args, ConsoleIo $io): ?int
     {
         $io->quiet('Quiet!');