Pulling out some duplicated code into methods.

This commit is contained in:
mark_story 2010-10-10 14:41:08 -04:00
parent 5e8222ef39
commit 8261581b3b
2 changed files with 24 additions and 10 deletions

View file

@ -53,6 +53,15 @@ class ConsoleInputOption {
}
}
/**
* Get the name of the argument
*
* @return string
*/
public function name() {
return $this->name;
}
/**
* Generate the help for this this option.
*

View file

@ -390,11 +390,7 @@ class ConsoleOptionParser {
}
if (!empty($this->_options)) {
$max = 0;
foreach ($this->_options as $description) {
$max = (strlen($description->name) > $max) ? strlen($description->name) : $max;
}
$max += 8;
$max = $this->_getMaxLength($this->_options) + 8;
$out[] = '<info>Options:</info>';
$out[] = '';
foreach ($this->_options as $option) {
@ -403,11 +399,7 @@ class ConsoleOptionParser {
$out[] = '';
}
if (!empty($this->_args)) {
$max = 0;
foreach ($this->_args as $argument) {
$max = (strlen($argument->name) > $max) ? strlen($argument->name) : $max;
}
$max += 2;
$max = $this->_getMaxLength($this->_args) + 2;
$out[] = '<info>Arguments:</info>';
$out[] = '';
foreach ($this->_args as $argument) {
@ -547,4 +539,17 @@ class ConsoleOptionParser {
protected function _nextToken() {
return isset($this->_tokens[0]) ? $this->_tokens[0] : '';
}
/**
* 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;
}
}