2013-10-22 14:26:45 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-10-22 14:26:45 +02:00
|
|
|
|
|
|
|
namespace Icinga\Cli;
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Params
|
|
|
|
*
|
|
|
|
* A class to ease commandline-option and -argument handling.
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
class Params
|
|
|
|
{
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* The name and path of the executable
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
protected $program;
|
2014-06-13 09:58:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The arguments
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
protected $standalone = array();
|
2014-06-13 09:58:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The options
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
protected $params = array();
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Parse the given commandline and create a new Params object
|
|
|
|
*
|
|
|
|
* @param array $argv The commandline
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function __construct($argv)
|
|
|
|
{
|
2014-06-13 09:58:50 +02:00
|
|
|
$noOptionFlag = false;
|
2013-10-22 14:26:45 +02:00
|
|
|
$this->program = array_shift($argv);
|
|
|
|
for ($i = 0; $i < count($argv); $i++) {
|
2014-06-13 09:58:50 +02:00
|
|
|
if ($argv[$i] === '--') {
|
|
|
|
$noOptionFlag = true;
|
|
|
|
} elseif (!$noOptionFlag && substr($argv[$i], 0, 2) === '--') {
|
2013-10-22 14:26:45 +02:00
|
|
|
$key = substr($argv[$i], 2);
|
|
|
|
if (! isset($argv[$i + 1]) || substr($argv[$i + 1], 0, 2) === '--') {
|
|
|
|
$this->params[$key] = true;
|
2014-06-13 09:58:50 +02:00
|
|
|
} elseif (array_key_exists($key, $this->params)) {
|
|
|
|
if (!is_array($this->params[$key])) {
|
|
|
|
$this->params[$key] = array($this->params[$key]);
|
|
|
|
}
|
|
|
|
$this->params[$key][] = $argv[++$i];
|
2013-10-22 14:26:45 +02:00
|
|
|
} else {
|
|
|
|
$this->params[$key] = $argv[++$i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->standalone[] = $argv[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Return the value for an argument by position
|
|
|
|
*
|
|
|
|
* @param int $pos The position of the argument
|
|
|
|
* @param mixed $default The default value to return
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function getStandalone($pos = 0, $default = null)
|
|
|
|
{
|
|
|
|
if (isset($this->standalone[$pos])) {
|
|
|
|
return $this->standalone[$pos];
|
|
|
|
}
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Count and return the number of arguments and options
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return count($this->standalone) + count($this->params);
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Return the options
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function getParams()
|
|
|
|
{
|
|
|
|
return $this->params;
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Return the arguments
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAllStandalone()
|
|
|
|
{
|
|
|
|
return $this->standalone;
|
|
|
|
}
|
|
|
|
|
2014-10-01 08:17:35 +02:00
|
|
|
/**
|
|
|
|
* Support isset() and empty() checks on options
|
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function __isset($name)
|
|
|
|
{
|
|
|
|
return isset($this->params[$name]);
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* @see Params::get()
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
return $this->get($key);
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Return whether the given option exists
|
|
|
|
*
|
|
|
|
* @param string $key The option name to check
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function has($key)
|
|
|
|
{
|
|
|
|
return array_key_exists($key, $this->params);
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Return the value of the given option
|
|
|
|
*
|
|
|
|
* @param string $key The option name
|
|
|
|
* @param mixed $default The default value to return
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function get($key, $default = null)
|
|
|
|
{
|
|
|
|
if ($this->has($key)) {
|
|
|
|
return $this->params[$key];
|
|
|
|
}
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Set a value for the given option
|
|
|
|
*
|
|
|
|
* @param string $key The option name
|
|
|
|
* @param mixed $value The value to set
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function set($key, $value)
|
|
|
|
{
|
|
|
|
$this->params[$key] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Remove a single option or multiple options
|
|
|
|
*
|
|
|
|
* @param string|array $keys The option or options to remove
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function remove($keys = array())
|
|
|
|
{
|
|
|
|
if (! is_array($keys)) {
|
|
|
|
$keys = array($keys);
|
|
|
|
}
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
if (array_key_exists($key, $this->params)) {
|
|
|
|
unset($this->params[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Return a copy of this object with the given options being removed
|
|
|
|
*
|
|
|
|
* @param string|array $keys The option or options to remove
|
|
|
|
*
|
|
|
|
* @return Params
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function without($keys = array())
|
|
|
|
{
|
|
|
|
$params = clone($this);
|
|
|
|
return $params->remove($keys);
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Remove and return the value of the given option
|
|
|
|
*
|
|
|
|
* Called multiple times for an option with multiple values returns
|
|
|
|
* them one by one in case the default is not an array.
|
|
|
|
*
|
|
|
|
* @param string $key The option name
|
|
|
|
* @param mixed $default The default value to return
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function shift($key = null, $default = null)
|
|
|
|
{
|
|
|
|
if ($key === null) {
|
|
|
|
if (count($this->standalone) > 0) {
|
|
|
|
return array_shift($this->standalone);
|
|
|
|
}
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
$result = $this->get($key, $default);
|
2014-06-13 09:58:50 +02:00
|
|
|
if (is_array($result) && !is_array($default)) {
|
|
|
|
$result = array_shift($result) || $default;
|
|
|
|
if ($result === $default) {
|
|
|
|
$this->remove($key);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->remove($key);
|
|
|
|
}
|
2013-10-22 14:26:45 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Put the given value onto the argument stack
|
|
|
|
*
|
|
|
|
* @param mixed $key The argument
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public function unshift($key)
|
|
|
|
{
|
|
|
|
array_unshift($this->standalone, $key);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-06-13 09:58:50 +02:00
|
|
|
/**
|
|
|
|
* Parse the given commandline
|
|
|
|
*
|
|
|
|
* @param array $argv The commandline to parse
|
|
|
|
*
|
|
|
|
* @return Params
|
|
|
|
*/
|
2013-10-22 14:26:45 +02:00
|
|
|
public static function parse($argv = null)
|
|
|
|
{
|
|
|
|
if ($argv === null) {
|
|
|
|
$argv = $GLOBALS['argv'];
|
|
|
|
}
|
|
|
|
$params = new self($argv);
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
}
|