cli: Add Params::shiftRequired()

This commit is contained in:
Eric Lippmann 2015-05-19 17:23:37 +02:00
parent ccaebd1d73
commit 338fad5da9
1 changed files with 24 additions and 0 deletions

View File

@ -258,6 +258,30 @@ class Params
return $result;
}
/**
* Require and remove a parameter
*
* @param string $name Name of the parameter
* @param bool $strict Whether the parameter's value must not be the empty string
*
* @return mixed
*
* @throws MissingParameterException If the parameter was not given
*/
public function shiftRequired($name, $strict = true)
{
if ($this->has($name)) {
$value = $this->get($name);
if (! $strict || strlen($value) > 0) {
$this->shift($name);
return $value;
}
}
$e = new MissingParameterException(t('Required parameter \'%s\' missing'), $name);
$e->setParameter($name);
throw $e;
}
/**
* Put the given value onto the argument stack
*