initial commit; version 22.5.12042

This commit is contained in:
2022-12-12 23:28:25 -05:00
commit af1b03d79f
17653 changed files with 22692970 additions and 0 deletions

View File

@ -0,0 +1,137 @@
<?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;
use cli\Memoize;
/**
* Represents an Argument or a value and provides several helpers related to parsing an argument list.
*/
class Argument extends Memoize {
/**
* The canonical name of this argument, used for aliasing.
*
* @param string
*/
public $key;
private $_argument;
private $_raw;
/**
* @param string $argument The raw argument, leading dashes included.
*/
public function __construct($argument) {
$this->_raw = $argument;
$this->key =& $this->_argument;
if ($this->isLong) {
$this->_argument = substr($this->_raw, 2);
} else if ($this->isShort) {
$this->_argument = substr($this->_raw, 1);
} else {
$this->_argument = $this->_raw;
}
}
/**
* Returns the raw input as a string.
*
* @return string
*/
public function __toString() {
return (string)$this->_raw;
}
/**
* Returns the formatted argument string.
*
* @return string
*/
public function value() {
return $this->_argument;
}
/**
* Returns the raw input.
*
* @return mixed
*/
public function raw() {
return $this->_raw;
}
/**
* Returns true if the string matches the pattern for long arguments.
*
* @return bool
*/
public function isLong() {
return (0 == strncmp($this->_raw, '--', 2));
}
/**
* Returns true if the string matches the pattern for short arguments.
*
* @return bool
*/
public function isShort() {
return !$this->isLong && (0 == strncmp($this->_raw, '-', 1));
}
/**
* Returns true if the string matches the pattern for arguments.
*
* @return bool
*/
public function isArgument() {
return $this->isShort() || $this->isLong();
}
/**
* Returns true if the string matches the pattern for values.
*
* @return bool
*/
public function isValue() {
return !$this->isArgument;
}
/**
* Returns true if the argument is short but contains several characters. Each
* character is considered a separate argument.
*
* @return bool
*/
public function canExplode() {
return $this->isShort && strlen($this->_argument) > 1;
}
/**
* Returns all but the first character of the argument, removing them from the
* objects representation at the same time.
*
* @return array
*/
public function exploded() {
$exploded = array();
for ($i = strlen($this->_argument); $i > 0; $i--) {
array_push($exploded, $this->_argument[$i - 1]);
}
$this->_argument = array_pop($exploded);
$this->_raw = '-' . $this->_argument;
return $exploded;
}
}

View File

@ -0,0 +1,126 @@
<?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;
use cli\Arguments;
/**
* Arguments help screen renderer
*/
class HelpScreen {
protected $_flags = array();
protected $_maxFlag = 0;
protected $_options = array();
protected $_maxOption = 0;
public function __construct(Arguments $arguments) {
$this->setArguments($arguments);
}
public function __toString() {
return $this->render();
}
public function setArguments(Arguments $arguments) {
$this->consumeArgumentFlags($arguments);
$this->consumeArgumentOptions($arguments);
}
public function consumeArgumentFlags(Arguments $arguments) {
$data = $this->_consume($arguments->getFlags());
$this->_flags = $data[0];
$this->_flagMax = $data[1];
}
public function consumeArgumentOptions(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("\n\n", $help);
}
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("\n", $help);
}
private function _consume($options) {
$max = 0;
$out = array();
foreach ($options as $option => $settings) {
if ($option[0] === '-') {
$names = array($option);
} else {
$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);
}
}

View File

@ -0,0 +1,43 @@
<?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;
/**
* Thrown when undefined arguments are detected in strict mode.
*/
class InvalidArguments extends \InvalidArgumentException {
protected $arguments;
/**
* @param array $arguments A list of arguments that do not fit the profile.
*/
public function __construct(array $arguments) {
$this->arguments = $arguments;
$this->message = $this->_generateMessage();
}
/**
* Get the arguments that caused the exception.
*
* @return array
*/
public function getArguments() {
return $this->arguments;
}
private function _generateMessage() {
return 'unknown argument' .
(count($this->arguments) > 1 ? 's' : '') .
': ' . join(', ', $this->arguments);
}
}

View File

@ -0,0 +1,123 @@
<?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;
use cli\Memoize;
class Lexer extends Memoize implements \Iterator {
private $_items = array();
private $_index = 0;
private $_length = 0;
private $_first = true;
/**
* @param array $items A list of strings to process as tokens.
*/
public function __construct(array $items) {
$this->_items = $items;
$this->_length = count($items);
}
/**
* The current token.
*
* @return string
*/
public function current() {
return $this->_item;
}
/**
* Peek ahead to the next token without moving the cursor.
*
* @return Argument
*/
public function peek() {
return new Argument($this->_items[0]);
}
/**
* Move the cursor forward 1 element if it is valid.
*/
public function next() {
if ($this->valid()) {
$this->_shift();
}
}
/**
* Return the current position of the cursor.
*
* @return int
*/
public function key() {
return $this->_index;
}
/**
* Move forward 1 element and, if the method hasn't been called before, reset
* the cursor's position to 0.
*/
public function rewind() {
$this->_shift();
if ($this->_first) {
$this->_index = 0;
$this->_first = false;
}
}
/**
* Returns true if the cursor has not reached the end of the list.
*
* @return bool
*/
public function valid() {
return ($this->_index < $this->_length);
}
/**
* Push an element to the front of the stack.
*
* @param mixed $item The value to set
*/
public function unshift($item) {
array_unshift($this->_items, $item);
$this->_length += 1;
}
/**
* Returns true if the cursor is at the end of the list.
*
* @return bool
*/
public function end() {
return ($this->_index + 1) == $this->_length;
}
private function _shift() {
$this->_item = new Argument(array_shift($this->_items));
$this->_index += 1;
$this->_explode();
$this->_unmemo('peek');
}
private function _explode() {
if (!$this->_item->canExplode) {
return false;
}
foreach ($this->_item->exploded as $piece) {
$this->unshift('-' . $piece);
}
}
}