-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.php
executable file
·98 lines (80 loc) · 2.38 KB
/
cli.php
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
<?php
/**
* iRedUtils CLI Script
*
* @author Peter Haider <[email protected]>
* @version 1.0
* @copyright Copyright 2014, Z
*/
include 'bootstrap.php';
/* Get the CLI arguements
--------------------------------------------------- */
function CLIarguments($args) {
$ret = array(
'exec' => '',
'options' => array(),
'flags' => array(),
'arguments' => array()
);
$ret['exec'] = array_shift($args);
while (($arg = array_shift($args)) != NULL) {
// Is it a option? (prefixed with --)
if ( substr($arg, 0, 2) === '--' ) {
$option = substr($arg, 2);
// is it the syntax '--option=argument'?
if (strpos($option,'=') !== FALSE)
array_push( $ret['options'], explode('=', $option, 2) );
else
array_push( $ret['options'], $option );
continue;
}
// Is it a flag or a serial of flags? (prefixed with -)
if ( substr( $arg, 0, 1 ) === '-' ) {
for ($i = 1; isset($arg[$i]) ; $i++)
$ret['flags'][] = $arg[$i];
continue;
}
// finally, it is not option, nor flag
$ret['arguments'][] = $arg;
continue;
}
return $ret;
}
try {
$cli = CLIarguments($argv);
if (!$cli['arguments']) {
echo 'No command specified! Usage:'."\n\n".' iredcli {command} [arguments]'."\n\n".'Commands:'."\n";
foreach (scandir(CMD_DIR) as $file) {
if (preg_match('/.php$/', $file))
echo "\n".' -> '.basename($file, '.php');
}
die("\n\n");
}
$commandName = array_shift($cli['arguments']);
$commandClass = ucfirst($commandName).'Command';
if (!file_exists(CMD_DIR.$commandName.'.php')) {
echo 'Command "'.$commandName.'" not found!'."\n";
die();
}
include_once(CMD_DIR.$commandName.'.php');
if (!class_exists($commandClass)) {
echo 'Command class "'.$commandClass.'" not found!'."\n";
die();
}
$options = array();
foreach ($cli['options'] as $opt) {
if (is_array($opt))
$options[$opt[0]] = $opt[1];
else {
if (substr($opt, 0, 3) == 'no-')
$options[substr($opt, 3)] = false;
else
$options[$opt] = true;
}
}
$cli['options'] = $options;
$cmd = new $commandClass($cli);
} catch (Exception $e) {
echo 'ERROR: '.$e->getMessage().' in '.$e->getFile().' Line '.$e->getLine()."\n";
echo 'TRACE:'."\n".$e->getTraceAsString()."\n";
}