Permalink
Browse files

Help generator for arguments

  • Loading branch information...
1 parent eeddb2b commit de0cdcdc82f78a529e8e0ee3d93ab3ec5ac2dc43 @jlogsdon committed Dec 17, 2012
Showing with 154 additions and 25 deletions.
  1. +11 −5 examples/arguments.php
  2. +23 −20 lib/cli/Arguments.php
  3. +120 −0 lib/cli/arguments/HelpScreen.php
@@ -17,15 +17,21 @@
$arguments = new \cli\Arguments(compact('strict'));
$arguments->addFlag(array('verbose', 'v'), 'Turn on verbose output');
-$arguments->addFlag('version', 'Turn on verbose output');
+$arguments->addFlag('version', 'Display the version');
$arguments->addFlag(array('quiet', 'q'), 'Disable all output');
+$arguments->addFlag(array('help', 'h'), 'Show this help screen');
$arguments->addOption(array('cache', 'C'), array(
- 'default' => __DIR__,
- 'description' => 'Set the cache directory. Defaults to the current directory'));
+ 'default' => getcwd(),
+ 'description' => 'Set the cache directory'));
$arguments->addOption(array('name', 'n'), array(
- 'default' => null,
- 'description' => 'Set a name.'));
+ 'default' => 'James',
+ 'description' => 'Set a name with a really long description and a default so we can see what line wrapping looks like which is probably a goo idea'));
$arguments->parse();
+if ($arguments['help']) {
+ echo $arguments->getHelpScreen();
+ echo "\n\n";
+}
+
echo $arguments->asJSON() . "\n";
View
@@ -18,7 +18,6 @@
class Arguments implements \ArrayAccess {
protected $_flags = array();
protected $_options = array();
- protected $_enableHelp = true;
protected $_strict = false;
protected $_input = array();
protected $_invalid = array();
@@ -36,13 +35,11 @@ class Arguments implements \ArrayAccess {
*/
public function __construct($options = array()) {
$options += array(
- 'help' => true,
'strict' => false,
'input' => array_slice($_SERVER['argv'], 1)
);
$this->_input = $options['input'];
- $this->setHelp($options['help']);
$this->setStrict($options['strict']);
if (isset($options['flags'])) {
@@ -65,6 +62,10 @@ public function getArguments() {
return $this->_parsed;
}
+ public function getHelpScreen() {
+ return new \cli\arguments\HelpScreen($this);
+ }
+
/**
* Encodes the parsed arguments as JSON.
*
@@ -99,7 +100,9 @@ public function offsetGet($offset) {
$offset = $offset->key;
}
- return $this->_parsed[$offset];
+ if (isset($this->_parsed[$offset])) {
+ return $this->_parsed[$offset];
+ }
}
/**
@@ -264,22 +267,6 @@ public function getInvalidArguments() {
}
/**
- * Enable or disable the generated help screen. If enabled, the flags `-h`
- * and `--help` will halt execution of the script and display a help screen
- * generated from the descriptions of each flag and option.
- *
- * *Note: with this option active, you cannot add the flags `-h` or
- * `--help`; they will be ignored.*
- *
- * @param bool $help True to enable, false to disable.
- * @return $this
- */
- public function setHelp($help) {
- $this->_enableHelp = (bool)$help;
- return $this;
- }
-
- /**
* Get a flag by primary matcher or any defined aliases.
*
* @param mixed $flag Either a string representing the flag or an
@@ -308,6 +295,14 @@ public function getFlag($flag) {
}
}
+ public function getFlags() {
+ return $this->_flags;
+ }
+
+ public function hasFlags() {
+ return !empty($this->_flags);
+ }
+
/**
* Returns true if the given argument is defined as a flag.
*
@@ -360,6 +355,14 @@ public function getOption($option) {
}
}
+ public function getOptions() {
+ return $this->_options;
+ }
+
+ public function hasOptions() {
+ return !empty($this->_options);
+ }
+
/**
* Returns true if the given argument is defined as an option.
*
@@ -0,0 +1,120 @@
+<?php
+/**
+ * PHP Command Line Tools
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this package in the file LICENSE.
+ *
+ * @author James Logsdon <dwarf@girsbrain.org>
+ * @copyright 2010 James Logsdom (http://girsbrain.org)
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+
+namespace cli\arguments;
+
+/**
+ * Arguments help screen renderer
+ */
+class HelpScreen {
+ protected $_flags = array();
+ protected $_maxFlag = 0;
+ protected $_options = array();
+ protected $_maxOption = 0;
+
+ public function __construct(\cli\Arguments $arguments) {
+ $this->setArguments($arguments);
+ }
+
+ public function __toString() {
+ return $this->render();
+ }
+
+ public function setArguments(\cli\Arguments $arguments) {
+ $this->consumeArgumentFlags($arguments);
+ $this->consumeArgumentOptions($arguments);
+ }
+
+ public function consumeArgumentFlags(\cli\Arguments $arguments) {
+ $data = $this->_consume($arguments->getFlags());
+
+ $this->_flags = $data[0];
+ $this->_flagMax = $data[1];
+ }
+
+ public function consumeArgumentOptions(\cli\Arguments $arguments) {
+ $data = $this->_consume($arguments->getOptions());
+
+ $this->_options = $data[0];
+ $this->_optionMax = $data[1];
+ }
+
+ public function render() {
+ $help = array();
+
+ array_push($help, $this->_renderFlags());
+ array_push($help, $this->_renderOptions());
+
+ return join($help, "\n\n");
+ }
+
+ private function _renderFlags() {
+ if (empty($this->_flags)) {
+ return null;
+ }
+
+ return "Flags\n" . $this->_renderScreen($this->_flags, $this->_flagMax);
+ }
+
+ private function _renderOptions() {
+ if (empty($this->_options)) {
+ return null;
+ }
+
+ return "Options\n" . $this->_renderScreen($this->_options, $this->_optionMax);
+ }
+
+ private function _renderScreen($options, $max) {
+ $help = array();
+ foreach ($options as $option => $settings) {
+ $formatted = ' ' . str_pad($option, $max);
+
+ $dlen = 80 - 4 - $max;
+
+ $description = str_split($settings['description'], $dlen);
+ $formatted.= ' ' . array_shift($description);
+
+ if ($settings['default']) {
+ $formatted .= ' [default: ' . $settings['default'] . ']';
+ }
+
+ $pad = str_repeat(' ', $max + 3);
+ while ($desc = array_shift($description)) {
+ $formatted .= "\n${pad}${desc}";
+ }
+
+ array_push($help, $formatted);
+ }
+
+ return join($help, "\n");
+ }
+
+ private function _consume($options) {
+ $max = 0;
+ $out = array();
+
+ foreach ($options as $option => $settings) {
+ $names = array('--' . $option);
+
+ foreach ($settings['aliases'] as $alias) {
+ array_push($names, '-' . $alias);
+ }
+
+ $names = join($names, ', ');
+ $max = max(strlen($names), $max);
+ $out[$names] = $settings;
+ }
+
+ return array($out, $max);
+ }
+}
+

0 comments on commit de0cdcd

Please sign in to comment.