Browse Source

Add alias setter to HelpFormatter

Michael Hoffmann 8 years ago
parent
commit
0b57ea656d

+ 21 - 0
src/Console/ConsoleOptionParser.php

@@ -135,6 +135,14 @@ class ConsoleOptionParser
     protected $_tokens = [];
 
     /**
+     * Help alias use in the HelpFormatter.
+     *
+     * @see \Cake\Console\HelpFormatter::setAlias()
+     * @var string
+     */
+    protected $_helpAlias = 'cake';
+
+    /**
      * Construct an OptionParser so you can define its behavior
      *
      * @param string|null $command The command name this parser is for. The command name is used for generating help.
@@ -737,6 +745,8 @@ class ConsoleOptionParser
         }
 
         $formatter = new HelpFormatter($this);
+        $formatter->setAlias($this->_helpAlias);
+
         if ($format === 'text') {
             return $formatter->text($width);
         }
@@ -746,6 +756,17 @@ class ConsoleOptionParser
     }
 
     /**
+     * Set the alias used in the HelpFormatter
+     *
+     * @param string $alias The alias
+     * @return void
+     */
+    public function setHelpAlias($alias)
+    {
+        $this->_helpAlias = $alias;
+    }
+
+    /**
      * Parse the value for a long option out of $this->_tokens. Will handle
      * options with an `=` in them.
      *

+ 25 - 2
src/Console/HelpFormatter.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\Console;
 
+use Cake\Console\Exception\ConsoleException;
 use Cake\Utility\Text;
 use SimpleXmlElement;
 
@@ -51,6 +52,13 @@ class HelpFormatter
     protected $_parser;
 
     /**
+     * Alias to display in the output.
+     *
+     * @var string
+     */
+    protected $_alias = 'cake';
+
+    /**
      * Build the help formatter for an OptionParser
      *
      * @param \Cake\Console\ConsoleOptionParser $parser The option parser help is being generated for.
@@ -61,6 +69,21 @@ class HelpFormatter
     }
 
     /**
+     * Set the alias
+     *
+     * @return void
+     * @throws \Cake\Console\Exception\ConsoleException When alias is not a string.
+     */
+    public function setAlias($alias)
+    {
+        if (is_string($alias)) {
+            $this->_alias = $alias;
+        } else {
+            throw new ConsoleException('Alias must be of type string.');
+        }
+    }
+
+    /**
      * Get the help as formatted text suitable for output on the command line.
      *
      * @param int $width The width of the help output.
@@ -91,7 +114,7 @@ class HelpFormatter
                 ]);
             }
             $out[] = '';
-            $out[] = sprintf('To see help on a subcommand use <info>`cake %s [subcommand] --help`</info>', $parser->getCommand());
+            $out[] = sprintf('To see help on a subcommand use <info>`' . $this->_alias . ' %s [subcommand] --help`</info>', $parser->getCommand());
             $out[] = '';
         }
 
@@ -142,7 +165,7 @@ class HelpFormatter
      */
     protected function _generateUsage()
     {
-        $usage = ['cake ' . $this->_parser->getCommand()];
+        $usage = [$this->_alias . ' ' . $this->_parser->getCommand()];
         $subcommands = $this->_parser->subcommands();
         if (!empty($subcommands)) {
             $usage[] = '[subcommand]';

+ 29 - 0
tests/TestCase/Console/HelpFormatterTest.php

@@ -294,6 +294,35 @@ xml;
     }
 
     /**
+     * Test setting a help alias
+     *
+     * @return void
+     */
+    public function testWithHelpAlias()
+    {
+        $parser = new ConsoleOptionParser('mycommand', false);
+        $formatter = new HelpFormatter($parser);
+        $formatter->setAlias('foo');
+        $result = $formatter->text();
+        $expected = 'foo mycommand [-h]';
+        $this->assertContains($expected, $result);
+    }
+
+    /**
+     * Tests that setting a none string help alias triggers an exception
+     *
+     * @expectedException \Cake\Console\Exception\ConsoleException
+     * @expectedExceptionMessage Alias must be of type string.
+     * @return void
+     */
+    public function testWithNoneStringHelpAlias()
+    {
+        $parser = new ConsoleOptionParser('mycommand', false);
+        $formatter = new HelpFormatter($parser);
+        $formatter->setAlias(['foo']);
+    }
+
+    /**
      * test help() with options and arguments that have choices.
      *
      * @return void