Navigation Menu

Skip to content

Commit

Permalink
Add alias setter to HelpFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hoffmann committed May 15, 2017
1 parent 0c5c24c commit 0b57ea6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/Console/ConsoleOptionParser.php
Expand Up @@ -134,6 +134,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
*
Expand Down Expand Up @@ -737,6 +745,8 @@ public function help($subcommand = null, $format = 'text', $width = 72)
}

$formatter = new HelpFormatter($this);
$formatter->setAlias($this->_helpAlias);

if ($format === 'text') {
return $formatter->text($width);
}
Expand All @@ -745,6 +755,17 @@ public function help($subcommand = null, $format = 'text', $width = 72)
}
}

/**
* 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.
Expand Down
27 changes: 25 additions & 2 deletions src/Console/HelpFormatter.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Console;

use Cake\Console\Exception\ConsoleException;
use Cake\Utility\Text;
use SimpleXmlElement;

Expand Down Expand Up @@ -50,6 +51,13 @@ class HelpFormatter
*/
protected $_parser;

/**
* Alias to display in the output.
*
* @var string
*/
protected $_alias = 'cake';

/**
* Build the help formatter for an OptionParser
*
Expand All @@ -60,6 +68,21 @@ public function __construct(ConsoleOptionParser $parser)
$this->_parser = $parser;
}

/**
* 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.
*
Expand Down Expand Up @@ -91,7 +114,7 @@ public function text($width = 72)
]);
}
$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[] = '';
}

Expand Down Expand Up @@ -142,7 +165,7 @@ public function text($width = 72)
*/
protected function _generateUsage()
{
$usage = ['cake ' . $this->_parser->getCommand()];
$usage = [$this->_alias . ' ' . $this->_parser->getCommand()];
$subcommands = $this->_parser->subcommands();
if (!empty($subcommands)) {
$usage[] = '[subcommand]';
Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/Console/HelpFormatterTest.php
Expand Up @@ -293,6 +293,35 @@ public function testHelpWithLotsOfArguments()
$this->assertContains($expected, $result);
}

/**
* 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.
*
Expand Down

0 comments on commit 0b57ea6

Please sign in to comment.