diff --git a/library/Icinga/Cli/Params.php b/library/Icinga/Cli/Params.php index 27a1df9a1..34dd5e177 100644 --- a/library/Icinga/Cli/Params.php +++ b/library/Icinga/Cli/Params.php @@ -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 *