-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhashbang.php
More file actions
123 lines (100 loc) · 3.23 KB
/
Copy pathhashbang.php
File metadata and controls
123 lines (100 loc) · 3.23 KB
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
<?php
class HashBang
{
protected $main;
protected $switches = array();
protected $args = array();
protected $requiredArgs = array();
protected $optionalArgs = array();
protected $argv = array();
protected $options = array();
public function __construct(\Closure $main)
{
$this->main = $main;
}
public function go()
{
try {
$this->initialize();
$this->invoke($this->main);
} catch (\HashBangException $e) {
$this->handleException($e);
}
}
public function addArg($arg, $required = true)
{
$this->args[$arg] = null;
if ($required) {
$this->requiredArgs[] = $arg;
} else {
$this->optionalArgs[] = $arg;
}
}
public function addSwitch($short, $long = null)
{
$switch = array();
$switch['short'] = $short;
$switch['long'] = $long;
$this->switches[] = $switch;
}
protected function invoke(\Closure $main)
{
$refl = new \ReflectionFunction($main);
$args = $this->args;
$args['argv'] = $this->argv;
$args['options'] = $this->options;
$argList = array();
$params = $refl->getParameters();
foreach ($params as $param) {
$argList[$param->getPosition()] = isset($args[$param->name]) ? $args[$param->name] : null;
}
$refl->invokeArgs($argList);
}
/**
* @return array values left in $argv after initialization
*/
protected function initialize()
{
$argv = $GLOBALS['argv'];
array_shift($argv);
$count = count($this->switches);
for($i = 0; $i < $count; $i++) {
$short = ltrim($this->switches[$i]['short'], '-');
$long = ltrim($this->switches[$i]['long'], '-');
$opts = getopt($short, array($long));
$value = null;
foreach ($opts as $opt => $value) {
$short = rtrim($short, ':');
$long = rtrim($long, ':');
$value = $value ?: true;
$prefix = 1 === strlen($opt) ? '-' : '--';
if (false !== $index = array_search($prefix.$opt, $argv)) {
unset($argv[$index]);
if (true !== $value) {
unset($argv[++$index]);
}
}
}
$this->options[$long] = $this->options[$short] = $value;
}
unset($this->options['']); // artifact from switches without a long version
while ($this->requiredArgs) {
$required = array_shift($this->requiredArgs);
$val = array_shift($argv);
if (null === $val) {
throw new \HashBangException(sprintf("'%s' is a required argument", $required));
}
$this->args[$required] = $val;
}
while ($this->optionalArgs) {
$optional = array_shift($this->optionalArgs);
$this->args[$optional] = array_shift($argv);
}
$this->argv = $argv;
}
protected function handleException(hashbangException $e)
{
echo "Error: {$e->getMessage()}\n";
}
}
class HashBangException extends \Exception {}