Skip to content

Commit

Permalink
Normalize on rootName instead of helpAlias.
Browse files Browse the repository at this point in the history
This makes the naming consistent with CommandRunner. I feel rootName is
more accurate vs. helpAlias as well.
  • Loading branch information
markstory committed Jun 30, 2017
1 parent 9db27f2 commit 261cdc3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Console/ConsoleOptionParser.php
Expand Up @@ -135,12 +135,12 @@ class ConsoleOptionParser
protected $_tokens = [];

/**
* Help alias used in the HelpFormatter.
* Root alias used in help output
*
* @see \Cake\Console\HelpFormatter::setAlias()
* @var string
*/
protected $_helpAlias = 'cake';
protected $rootName = 'cake';

/**
* Construct an OptionParser so you can define its behavior
Expand Down Expand Up @@ -719,6 +719,7 @@ public function parse($argv)

/**
* Gets formatted help for this parser object.
*
* Generates help text based on the description, options, arguments, subcommands and epilog
* in the parser.
*
Expand All @@ -745,7 +746,7 @@ public function help($subcommand = null, $format = 'text', $width = 72)
}

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

if ($format === 'text') {
return $formatter->text($width);
Expand All @@ -760,10 +761,24 @@ public function help($subcommand = null, $format = 'text', $width = 72)
*
* @param string $alias The alias
* @return void
* @deprecated 3.5.0 Use setRootName() instead.
*/
public function setHelpAlias($alias)
{
$this->_helpAlias = $alias;
$this->rootName = $alias;
}

/**
* Set the root name used in the HelpFormatter
*
* @param string $name The root command name
* @return void
*/
public function setRootName($name)
{
$this->rootName = (string)$name;

return $this;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/TestCase/Console/ConsoleOptionParserTest.php
Expand Up @@ -766,6 +766,34 @@ public function testHelpSubcommandHelpArray()
--quiet, -q Enable quiet output.
--verbose, -v Enable verbose output.
TEXT;
$this->assertTextEquals($expected, $result, 'Help is not correct.');
}

/**
* test that help() with a custom rootName
*
* @return void
*/
public function testHelpWithRootName()
{
$parser = new ConsoleOptionParser('sample', false);
$parser->description('A command!')
->setRootName('tool')
->addOption('test', ['help' => 'A test option.']);

$result = $parser->help();
$expected = <<<TEXT
A command!
<info>Usage:</info>
tool sample [-h] [--test]
<info>Options:</info>
--help, -h Display this help.
--test A test option.
TEXT;
$this->assertTextEquals($expected, $result, 'Help is not correct.');
}
Expand Down

0 comments on commit 261cdc3

Please sign in to comment.