Skip to content

Commit

Permalink
lib: Add Params::req()
Browse files Browse the repository at this point in the history
Params::req() should be used for requiring a mandatory CLI parameter.

refs #8886
  • Loading branch information
lippserd committed Apr 7, 2015
1 parent 93b1a35 commit 002e793
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions library/Icinga/Cli/Params.php
Expand Up @@ -3,6 +3,8 @@

namespace Icinga\Cli;

use Icinga\Exception\MissingParameterException;

/**
* Params
*
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 002e793

Please sign in to comment.