Skip to content

Instantly share code, notes, and snippets.

@mageekguy
Created May 11, 2014 20:16
Show Gist options
  • Save mageekguy/82c64af991e3b9da8149 to your computer and use it in GitHub Desktop.
Save mageekguy/82c64af991e3b9da8149 to your computer and use it in GitHub Desktop.
<?php
namespace jobs\script\configuration\cli;
use
jobs\world
;
class parser
{
private $callable = null;
function __construct(callable $callable)
{
$this->callable = $callable;
}
function parse(array $values)
{
$option = null;
$arguments = [];
foreach ($values as $value)
{
if (static::isOption($value) === false)
{
$arguments[] = $value;
}
else
{
if ($option !== null)
{
$this->callCallable($option, $arguments);
}
$option = $value;
$arguments = [];
}
}
if ($option !== null)
{
$this->callCallable($option, $arguments);
}
return $this;
}
private function callCallable($option, $arguments)
{
call_user_func_array($this->callable, array($option, $arguments));
return $this;
}
private static function isOption($value)
{
return preg_match('/^(\+{1,}|-{1,})[a-z][-_a-z0-9]*/i', $value) === 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment