Skip to content

Commit

Permalink
Moving code into HelpFormatter to help separate the help generation f…
Browse files Browse the repository at this point in the history
…rom the option parser.
  • Loading branch information
markstory committed Oct 20, 2010
1 parent 1954c7c commit 4e44ff9
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 60 deletions.
61 changes: 3 additions & 58 deletions cake/console/libs/console_option_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require_once CONSOLE_LIBS . 'console_input_option.php';
require_once CONSOLE_LIBS . 'console_input_argument.php';
require_once CONSOLE_LIBS . 'console_input_subcommand.php';
require_once CONSOLE_LIBS . 'help_formatter.php';

/**
* Handles parsing the ARGV in the command line and provides support
Expand Down Expand Up @@ -478,64 +479,8 @@ public function help($subcommand = null, $width = 72) {
$subparser->command($this->command() . ' ' . $subparser->command());
return $subparser->help();
}
$out = array();
if (!empty($this->_description)) {
$out[] = String::wrap($this->_description, $width);
$out[] = '';
}
$out[] = '<info>Usage:</info>';
$out[] = $this->_generateUsage();
$out[] = '';
if (!empty($this->_subcommands)) {
$out[] = '<info>Subcommands:</info>';
$out[] = '';
$max = $this->_getMaxLength($this->_subcommands) + 2;
foreach ($this->_subcommands as $command) {
$out[] = String::wrap($command->help($max), array(
'width' => $width,
'indent' => str_repeat(' ', $max),
'indentAt' => 1
));
}
$out[] = '';
$out[] = sprintf(
__('To see help on a subcommand use <info>`cake %s [subcommand] --help`</info>'),
$this->command()
);
$out[] = '';
}

if (!empty($this->_options)) {
$max = $this->_getMaxLength($this->_options) + 8;
$out[] = '<info>Options:</info>';
$out[] = '';
foreach ($this->_options as $option) {
$out[] = String::wrap($option->help($max), array(
'width' => $width,
'indent' => str_repeat(' ', $max),
'indentAt' => 1
));
}
$out[] = '';
}
if (!empty($this->_args)) {
$max = $this->_getMaxLength($this->_args) + 2;
$out[] = '<info>Arguments:</info>';
$out[] = '';
foreach ($this->_args as $argument) {
$out[] = String::wrap($argument->help($max), array(
'width' => $width,
'indent' => str_repeat(' ', $max),
'indentAt' => 1
));
}
$out[] = '';
}
if (!empty($this->_epilog)) {
$out[] = String::wrap($this->_epilog, $width);
$out[] = '';
}
return implode("\n", $out);
$formatter = new HelpFormatter($this);
return $formatter->text($width);
}

/**
Expand Down
103 changes: 101 additions & 2 deletions cake/console/libs/help_formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,107 @@ public function __construct(ConsoleOptionParser $parser) {
*
* @return string
*/
public function text() {

public function text($width) {
$parser = $this->_parser;
$out = array();
$description = $parser->description();
if (!empty($description)) {
$out[] = String::wrap($description, $width);
$out[] = '';
}
$out[] = '<info>Usage:</info>';
$out[] = $this->_generateUsage();
$out[] = '';
$subcommands = $parser->subcommands();
if (!empty($subcommands)) {
$out[] = '<info>Subcommands:</info>';
$out[] = '';
$max = $this->_getMaxLength($subcommands) + 2;
foreach ($subcommands as $command) {
$out[] = String::wrap($command->help($max), array(
'width' => $width,
'indent' => str_repeat(' ', $max),
'indentAt' => 1
));
}
$out[] = '';
$out[] = sprintf(
__('To see help on a subcommand use <info>`cake %s [subcommand] --help`</info>'),
$parser->command()
);
$out[] = '';
}

$options = $parser->options();
if (!empty($options)) {
$max = $this->_getMaxLength($options) + 8;
$out[] = '<info>Options:</info>';
$out[] = '';
foreach ($options as $option) {
$out[] = String::wrap($option->help($max), array(
'width' => $width,
'indent' => str_repeat(' ', $max),
'indentAt' => 1
));
}
$out[] = '';
}

$arguments = $parser->arguments();
if (!empty($arguments)) {
$max = $this->_getMaxLength($arguments) + 2;
$out[] = '<info>Arguments:</info>';
$out[] = '';
foreach ($arguments as $argument) {
$out[] = String::wrap($argument->help($max), array(
'width' => $width,
'indent' => str_repeat(' ', $max),
'indentAt' => 1
));
}
$out[] = '';
}
$epilog = $parser->epilog();
if (!empty($epilog)) {
$out[] = String::wrap($epilog, $width);
$out[] = '';
}
return implode("\n", $out);
}

/**
* Generate the usage for a shell based on its arguments and options.
* Usage strings favour short options over the long ones. and optional args will
* be indicated with []
*
* @return string
*/
protected function _generateUsage() {
$usage = array('cake ' . $this->_parser->command());
$subcommands = $this->_parser->subcommands();
if (!empty($subcommands)) {
$usage[] = '[subcommand]';
}
foreach ($this->_parser->options() as $option) {
$usage[] = $option->usage();
}
foreach ($this->_parser->arguments() as $argument) {
$usage[] = $argument->usage();
}
return implode(' ', $usage);
}

/**
* Iterate over a collection and find the longest named thing.
*
* @return integer
*/
protected function _getMaxLength($collection) {
$max = 0;
foreach ($collection as $item) {
$max = (strlen($item->name()) > $max) ? strlen($item->name()) : $max;
}
return $max;
}

/**
Expand Down

0 comments on commit 4e44ff9

Please sign in to comment.