-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathclass.args.php
101 lines (91 loc) · 3.18 KB
/
class.args.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
99
100
101
<?PHP
// A quick and dirty command line argument parser. Written in about
// an hour, so you might want to take this with a grain of salt or two.
//
// More info:
// http://clickontyler.com/blog/2008/11/parse-command-line-arguments-in-php/
//
// Single letter options should be prefixed with a single
// dash and can be grouped together. Examples:
//
// cmd -a
// cmd -ab
//
// Values can be assigned to single letter options like so:
//
// cmd -a foo (a will be set to foo.)
//
// cmd -a foo -b (a will be set to foo.)
//
// cmd -ab foo (a and b will simply be set to true. foo is only listed as an argument.)
//
// You can also use the double-dash syntax. Examples:
//
// cmd --value
//
// cmd --value foo (value is set to foo)
//
// cmd --value=foo (value is set to foo)
//
// Single dash and double dash syntax may be mixed.
//
// Trailing arguments are treated as such. Examples:
//
// cmd -abc foo bar (foo and bar are listed as arguments)
//
// cmd -a foo -b bar charlie (only bar and charlie are arguments)
class Args
{
private $flags;
public $args;
public function __construct()
{
$this->flags = array();
$this->args = array();
$argv = $GLOBALS['argv'];
array_shift($argv);
for($i = 0; $i < count($argv); $i++)
{
$str = $argv[$i];
// --foo
if(strlen($str) > 2 && substr($str, 0, 2) == '--')
{
$str = substr($str, 2);
$parts = explode('=', $str);
$this->flags[$parts[0]] = true;
// Does not have an =, so choose the next arg as its value
if(count($parts) == 1 && isset($argv[$i + 1]) && preg_match('/^--?.+/', $argv[$i + 1]) == 0)
{
$this->flags[$parts[0]] = $argv[$i + 1];
}
elseif(count($parts) == 2) // Has a =, so pick the second piece
{
$this->flags[$parts[0]] = $parts[1];
}
}
elseif(strlen($str) == 2 && $str[0] == '-') // -a
{
$this->flags[$str[1]] = true;
if(isset($argv[$i + 1]) && preg_match('/^--?.+/', $argv[$i + 1]) == 0)
$this->flags[$str[1]] = $argv[$i + 1];
}
elseif(strlen($str) > 1 && $str[0] == '-') // -abcdef
{
for($j = 1; $j < strlen($str); $j++)
$this->flags[$str[$j]] = true;
}
}
for($i = count($argv) - 1; $i >= 0; $i--)
{
if(preg_match('/^--?.+/', $argv[$i]) == 0)
$this->args[] = $argv[$i];
else
break;
}
$this->args = array_reverse($this->args);
}
public function flag($name)
{
return isset($this->flags[$name]) ? $this->flags[$name] : false;
}
}