From 002e793a536efea9da7a3bad99250ce1e9d5656c Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 7 Apr 2015 12:20:37 +0200 Subject: [PATCH] lib: Add Params::req() Params::req() should be used for requiring a mandatory CLI parameter. refs #8886 --- library/Icinga/Cli/Params.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/library/Icinga/Cli/Params.php b/library/Icinga/Cli/Params.php index 27a1df9a1c..34dd5e177f 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 @@ public function get($key, $default = null) 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 *