Browse files

Fixed cli usage output alignment

  • Loading branch information...
1 parent ad5d37d commit 6450ee6cb4696c21c62c7cb13bec729acd46a623 @brunnels committed Feb 8, 2013
Showing with 81 additions and 15 deletions.
  1. +1 −1 generate.php
  2. +72 −7 lib/cli/Cli.php
  3. +8 −7 lib/cli/Flag.php
View
2 generate.php
@@ -61,7 +61,7 @@ function gettext($str)
$cli->addFlag('-e', _('If all classes should be guarded with if(!class_exists) statements'), true, false);
$cli->addFlag('-t', _('If no type constructor should be generated'), true, false);
$cli->addFlag('-s', _('If the output should be a single file'), true, false);
-$cli->addFlag('-d', _('Skips addiing dependency includes'), true, false);
+$cli->addFlag('-d', _('Skips adding dependency includes'), true, false);
$cli->addFlag('-g', _('The service class name'), false, false);
$cli->addFlag('-v', _('If the output to the console should be verbose'), true, false);
$cli->addFlag('-i', _('The input wsdl file'), false, true);
View
79 lib/cli/Cli.php
@@ -38,7 +38,7 @@ class Cli extends CliParser
/**
*
- * @var array An array of accepted Flags, all the flags that have a meaning
+ * @var array[Flag] An array of accepted Flags, all the flags that have a meaning
*/
private $acceptedFlags;
@@ -47,14 +47,16 @@ class Cli extends CliParser
* @var array An array of the requred Flag objects
*/
private $requiredFlags;
+
+ private $size;
/**
*
* @param string $programName
* @param string $usage
* @param string $version
*/
- public function __construct($programName, $usage, $version)
+ public function __construct($programName, $usage, $version, $maxLineSize = null)
{
parent::__construct();
@@ -63,6 +65,21 @@ public function __construct($programName, $usage, $version)
$this->version = $version;
$this->acceptedFlags = array();
$this->requiredFlags = array();
+
+ if (null === $maxLineSize)
+ {
+ if (function_exists('shell_exec'))
+ {
+ // this is tricky because "tput cols 2>&1" is not accurate
+ $maxLineSize = ctype_digit(trim(shell_exec('tput cols 2>&1'))) ? (integer) shell_exec('tput cols') : 78;
+ }
+ else
+ {
+ $maxLineSize = 78;
+ }
+ }
+
+ $this->size = $maxLineSize;
}
/**
@@ -144,19 +161,67 @@ public function addAlias($flag, $alias)
*/
public function showUsage()
{
+ print PHP_EOL;
print _('Usage: ').$this->programName.' '.$this->usageString.PHP_EOL;
- foreach ($this->acceptedFlags as $flag)
- {
- print $flag;
- }
+ $this->printFlagsUsageText();
+
+ print PHP_EOL;
print _('Version: ').$this->version.PHP_EOL;
print PHP_EOL;
exit;
}
-
+
+ /**
+ *
+ * @return array[Flag]
+ */
+ private function getAcceptedFlags()
+ {
+ return $this->acceptedFlags;
+ }
+
+ private function printFlagsUsageText()
+ {
+ $max = 0;
+
+ // find the largest name string
+ foreach ($this->getAcceptedFlags() as $flag)
+ {
+ $max = (strlen($flag->getName()) + 2 > $max) ? strlen($flag->getName()) + 2 : $max;
+ }
+
+ foreach($this->getAcceptedFlags() as $flag)
+ {
+
+ $flagStr = sprintf(
+ ' %-'.$max.'s %s%s',
+ $flag->getName(),
+ $flag->getAliasStr() ? sprintf('( %s ) ', $flag->getAliasStr()) : '',
+ $flag->getDescription()
+ );
+
+ if(strlen($flagStr) > $this->size)
+ {
+ while((strlen($flagStr) + $max) > $this->size)
+ {
+ $lines = explode("\n", wordwrap($flagStr, $this->size, "\n"));
+ print array_shift($lines);
+ $flagStr = sprintf('%-' . ($max + 2) .'s','') . implode(' ', $lines);
+ }
+
+ print PHP_EOL.$flagStr;
+ }
+ else
+ {
+ print $flagStr;
+ }
+ print PHP_EOL;
+ }
+ }
+
/**
* Parses and validates the flags according to the rules set up
* Shows usage and terminates if the validation fails
View
15 lib/cli/Flag.php
@@ -56,15 +56,16 @@ public function __construct($name, $description, $isBool = false)
$this->aliases = array();
$this->maxNumAliases = 4;
}
-
- /**
- *
- * @return string Returns the string representation of the flag
- */
- public function __toString()
+
+ public function getDescription()
{
- return implode(', ', array_merge(array($this->name), $this->aliases)).str_pad('', $this->maxNumAliases - count($this->aliases), "t").$this->description.PHP_EOL;
+ return $this->description;
}
+
+ public function getAliasStr()
+ {
+ return implode(', ', $this->getAliases());
+ }
/**
*

0 comments on commit 6450ee6

Please sign in to comment.