summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Cli/Params.php
blob: 463d4aecf86b1d23165c1c9d3d4eac48644dca42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Cli;

use Icinga\Exception\MissingParameterException;

/**
 * Params
 *
 * A class to ease commandline-option and -argument handling.
 */
class Params
{
    /**
     * The name and path of the executable
     *
     * @var string
     */
    protected $program;

    /**
     * The arguments
     *
     * @var array
     */
    protected $standalone = array();

    /**
     * The options
     *
     * @var array
     */
    protected $params = array();

    /**
     * Parse the given commandline and create a new Params object
     *
     * @param   array   $argv   The commandline
     */
    public function __construct($argv)
    {
        $noOptionFlag = false;
        $this->program = array_shift($argv);
        for ($i = 0; $i < count($argv); $i++) {
            if ($argv[$i] === '--') {
                $noOptionFlag = true;
            } elseif (!$noOptionFlag && substr($argv[$i], 0, 2) === '--') {
                $key = substr($argv[$i], 2);
                $matches = array();
                if (1 === preg_match(
                    '/(?<!.)([^=]+)=(.*)(?!.)/ms',
                    $key,
                    $matches
                )) {
                    $this->params[$matches[1]] = $matches[2];
                } elseif (! isset($argv[$i + 1]) || substr($argv[$i + 1], 0, 2) === '--') {
                    $this->params[$key] = true;
                } 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];
                } else {
                    $this->params[$key] = $argv[++$i];
                }
            } else {
                $this->standalone[] = $argv[$i];
            }
        }
    }

    /**
     * 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
     */
    public function getStandalone($pos = 0, $default = null)
    {
        if (isset($this->standalone[$pos])) {
            return $this->standalone[$pos];
        }
        return $default;
    }

    /**
     * Count and return the number of arguments and options
     *
     * @return int
     */
    public function count()
    {
        return count($this->standalone) + count($this->params);
    }

    /**
     * Return the options
     *
     * @return array
     */
    public function getParams()
    {
        return $this->params;
    }

    /**
     * Return the arguments
     *
     * @return array
     */
    public function getAllStandalone()
    {
        return $this->standalone;
    }

    /**
     * Support isset() and empty() checks on options
     *
     * @param   $name
     *
     * @return  bool
     */
    public function __isset($name)
    {
        return isset($this->params[$name]);
    }

    /**
     * @see Params::get()
     */
    public function __get($key)
    {
        return $this->get($key);
    }

    /**
     * Return whether the given option exists
     *
     * @param   string  $key    The option name to check
     *
     * @return  bool
     */
    public function has($key)
    {
        return array_key_exists($key, $this->params);
    }

    /**
     * Return the value of the given option
     *
     * @param   string  $key        The option name
     * @param   mixed   $default    The default value to return
     *
     * @return  mixed
     */
    public function get($key, $default = null)
    {
        if ($this->has($key)) {
            return $this->params[$key];
        }
        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 getRequired($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
     *
     * @param   string  $key    The option name
     * @param   mixed   $value  The value to set
     *
     * @return  $this
     */
    public function set($key, $value)
    {
        $this->params[$key] = $value;
        return $this;
    }

    /**
     * Remove a single option or multiple options
     *
     * @param   string|array    $keys   The option or options to remove
     *
     * @return  $this
     */
    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;
    }

    /**
     * Return a copy of this object with the given options being removed
     *
     * @param   string|array    $keys   The option or options to remove
     *
     * @return  Params
     */
    public function without($keys = array())
    {
        $params = clone($this);
        return $params->remove($keys);
    }

    /**
     * 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
     */
    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);
        if (is_array($result) && !is_array($default)) {
            $result = array_shift($result) || $default;
            if ($result === $default) {
                $this->remove($key);
            }
        } else {
            $this->remove($key);
        }
        return $result;
    }

    /**
     * Require and remove 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 shiftRequired($name, $strict = true)
    {
        if ($this->has($name)) {
            $value = $this->get($name);
            if (! $strict || strlen($value) > 0) {
                $this->shift($name);
                return $value;
            }
        }
        $e = new MissingParameterException(t('Required parameter \'%s\' missing'), $name);
        $e->setParameter($name);
        throw $e;
    }

    /**
     * Put the given value onto the argument stack
     *
     * @param   mixed   $key    The argument
     *
     * @return  $this
     */
    public function unshift($key)
    {
        array_unshift($this->standalone, $key);
        return $this;
    }

    /**
     * Parse the given commandline
     *
     * @param   array   $argv   The commandline to parse
     *
     * @return  Params
     */
    public static function parse($argv = null)
    {
        if ($argv === null) {
            $argv = $GLOBALS['argv'];
        }
        $params = new self($argv);
        return $params;
    }
}