1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ _ _ _ |
5
|
|
|
* _ __ (_)__| |_ ___| | | |
6
|
|
|
* | ' \| (_-< ' \/ -_) | | |
7
|
|
|
* |_|_|_|_/__/_||_\___|_|_| |
8
|
|
|
* |
9
|
|
|
* This file is part of Kristuff\Mishell. |
10
|
|
|
* (c) Kr1s7uff For the full copyright and license information, |
11
|
|
|
* please view the LICENSE file that was distributed with this |
12
|
|
|
* source code. |
13
|
|
|
* |
14
|
|
|
* @version 1.6.2 |
15
|
|
|
* @copyright 2017-2024 Kr157uff |
16
|
|
|
*/ |
17
|
|
|
declare(ticks = 1); // Allow posix signal handling |
18
|
|
|
|
19
|
|
|
namespace Kristuff\Mishell; |
20
|
|
|
|
21
|
|
|
class Program |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Exit with given exit code |
25
|
|
|
* |
26
|
|
|
* @access public |
27
|
|
|
* @static |
28
|
|
|
* @param bool $code The exit code. Default is 0. 1 is generaly set for error. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public static function exit(int $code = 0) { |
33
|
|
|
exit($code); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* helper function to check if a argument is given |
38
|
|
|
* |
39
|
|
|
* @access protected |
40
|
|
|
* @static |
41
|
|
|
* @param array $arguments The list of arguments |
42
|
|
|
* @param string $shortArg The short argument to check |
43
|
|
|
* @param string $longArg The long argument to check |
44
|
|
|
* |
45
|
|
|
* @return bool True if the short or long argument exist in the arguments array, otherwise false |
46
|
|
|
*/ |
47
|
|
|
public static function inArguments(array $arguments, string $shortArg, string $longArg) |
48
|
|
|
{ |
49
|
|
|
return array_key_exists($shortArg, $arguments) || array_key_exists($longArg, $arguments); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* helper function to get the value of an argument |
54
|
|
|
* |
55
|
|
|
* |
56
|
|
|
* @access protected |
57
|
|
|
* @static |
58
|
|
|
* @param array $arguments The list of arguments |
59
|
|
|
* @param string $shortArg The short argument name to check |
60
|
|
|
* @param string $longArg The long argument name to check |
61
|
|
|
* |
62
|
|
|
* @return string|null |
63
|
|
|
* |
64
|
|
|
*/ |
65
|
|
|
public static function getArgumentValue(array $arguments, string $shortArg, string $longArg):? string |
66
|
|
|
{ |
67
|
|
|
return array_key_exists($shortArg, $arguments) ? $arguments[$shortArg] : |
68
|
|
|
(array_key_exists($longArg, $arguments) ? $arguments[$longArg] : null); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.