lib: Add Params::req()

Params::req() should be used for requiring a mandatory CLI parameter.

refs #8886
This commit is contained in:
Eric Lippmann 2015-04-07 12:20:37 +02:00
parent 93b1a35de0
commit 002e793a53
1 changed files with 25 additions and 0 deletions

View File

@ -3,6 +3,8 @@
namespace Icinga\Cli;
use Icinga\Exception\MissingParameterException;
/**
* Params
*
@ -155,6 +157,29 @@ class Params
return $default;
}
/**
* Require 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 req($name, $strict = true)
{
if ($this->has($name)) {
$value = $this->get($name);
if (! $strict || strlen($value) > 0) {
return $value;
}
}
$e = new MissingParameterException(t('Required parameter \'%s\' missing'), $name);
$e->setParameter($name);
throw $e;
}
/**
* Set a value for the given option
*