1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73:
<?php
/** Cz CLI Console
*
* @author Jan Pecha, <janpecha@email.cz>
*/
namespace Cz\Cli\Parameters;
use Cz\Cli\ParametersParseException;
class DefaultParametersParser extends BaseParser
{
protected function parse(array $raw = NULL)
{
$parameters = NULL;
// parsing
$lastName = NULL;
if(isset($raw[1])) // count($raw) > 1
{
// remove argv[0]
array_shift($raw);
// parsing
foreach($raw as $argument)
{
if($argument{0} === '-')
{
$name = trim($argument, '-');
$lastName = $name;
if(!isset($parameters[$name]))
{
$parameters[$name] = TRUE;
}
}
elseif($lastName === NULL)
{
$parameters[] = $argument;
#throw new ParametersParseException("Bad argument '$argument'");
}
else
{
if($parameters[$lastName] === TRUE)
{
$parameters[$lastName] = $argument;
}
else // string || array
{
if(is_string($parameters[$lastName]))
{
$parameters[$lastName] = array(
$parameters[$lastName],
$argument,
);
}
else
{
$parameters[$lastName][] = $argument;
}
$lastName = NULL;
}
}
}
}
// after parsing
$this->setParameters($parameters);
}
}