Skip to content

Commit

Permalink
Fixed cli usage output alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
brunnels committed Feb 8, 2013
1 parent ad5d37d commit 6450ee6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 15 deletions.
2 changes: 1 addition & 1 deletion generate.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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('-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('-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('-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('-g', _('The service class name'), false, false);
$cli->addFlag('-v', _('If the output to the console should be verbose'), true, false); $cli->addFlag('-v', _('If the output to the console should be verbose'), true, false);
$cli->addFlag('-i', _('The input wsdl file'), false, true); $cli->addFlag('-i', _('The input wsdl file'), false, true);
Expand Down
79 changes: 72 additions & 7 deletions lib/cli/Cli.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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; private $acceptedFlags;


Expand All @@ -47,14 +47,16 @@ class Cli extends CliParser
* @var array An array of the requred Flag objects * @var array An array of the requred Flag objects
*/ */
private $requiredFlags; private $requiredFlags;

private $size;


/** /**
* *
* @param string $programName * @param string $programName
* @param string $usage * @param string $usage
* @param string $version * @param string $version
*/ */
public function __construct($programName, $usage, $version) public function __construct($programName, $usage, $version, $maxLineSize = null)
{ {
parent::__construct(); parent::__construct();


Expand All @@ -63,6 +65,21 @@ public function __construct($programName, $usage, $version)
$this->version = $version; $this->version = $version;
$this->acceptedFlags = array(); $this->acceptedFlags = array();
$this->requiredFlags = 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;
} }


/** /**
Expand Down Expand Up @@ -144,19 +161,67 @@ public function addAlias($flag, $alias)
*/ */
public function showUsage() public function showUsage()
{ {
print PHP_EOL;
print _('Usage: ').$this->programName.' '.$this->usageString.PHP_EOL; print _('Usage: ').$this->programName.' '.$this->usageString.PHP_EOL;


foreach ($this->acceptedFlags as $flag) $this->printFlagsUsageText();
{
print $flag; print PHP_EOL;
}


print _('Version: ').$this->version.PHP_EOL; print _('Version: ').$this->version.PHP_EOL;


print PHP_EOL; print PHP_EOL;
exit; 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 * Parses and validates the flags according to the rules set up
* Shows usage and terminates if the validation fails * Shows usage and terminates if the validation fails
Expand Down
15 changes: 8 additions & 7 deletions lib/cli/Flag.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ public function __construct($name, $description, $isBool = false)
$this->aliases = array(); $this->aliases = array();
$this->maxNumAliases = 4; $this->maxNumAliases = 4;
} }


/** public function getDescription()
*
* @return string Returns the string representation of the flag
*/
public function __toString()
{ {
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());
}


/** /**
* *
Expand Down

0 comments on commit 6450ee6

Please sign in to comment.