forked from petewarden/buzzprofilecrawl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcliargs.php
162 lines (140 loc) · 3.44 KB
/
cliargs.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
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
<?php
// A set of utility functions to make handling command line arguments in PHP easier
// To use them, pass in an array describing the expected arguments, in the form
//
// array(
// '<long name of argument>' => array(
// 'short' => '<single letter version of argument>',
// 'type' => <'switch' | 'optional' | 'required'>,
// 'description' => '<help text for the argument>',
// 'default' => '<value if this is an optional argument and it isn't specified>',
// ),
// ...
// );
//
// If the type is switch, then the result is a boolean that will be false if it's
// not present, or true if it is
//
// If the type is optional, then the result will be the default if it's not present
//
// If the type is required, then the script will print out the usage and exit if it's
// not found
//
// To use, call cliargs_print_usage_and_exit() with the array of argument descriptions
// The result will be an array with the argument names as keys to the found values
//
// by Pete Warden, http://petewarden.typepad.com but freely reusable with no restrictions
function cliargs_print_usage_and_exit($cliargs)
{
print "Usage:\n";
foreach ($cliargs as $long => $arginfo)
{
$short = $arginfo['short'];
$type = $arginfo['type'];
$required = ($type=='required');
$optional = ($type=='optional');
$description = $arginfo['description'];
print "-$short/--$long ";
if ($optional||$required)
print "<value> ";
print ": $description";
if ($required)
print " (required)";
print "\n";
}
exit();
}
function cliargs_strstartswith($source, $prefix)
{
return strncmp($source, $prefix, strlen($prefix)) == 0;
}
function cliargs_get_options($cliargs)
{
global $argv;
global $argc;
$options = array('unnamed' => array());
for ($index=1; $index<$argc; $index+=1)
{
$currentarg = strtolower($argv[$index]);
$argparts = split('=', $currentarg);
$namepart = $argparts[0];
if (cliargs_strstartswith($namepart, '--'))
{
$longname = substr($namepart, 2);
}
else if (cliargs_strstartswith($namepart, '-'))
{
$shortname = substr($namepart, 1);
$longname = $shortname;
foreach ($cliargs as $name => $info)
{
if ($shortname===$info['short'])
{
$longname = $name;
break;
}
}
}
else
{
$longname = 'unnamed';
}
if ($longname=='unnamed')
{
$options['unnamed'][] = $namepart;
}
else
{
if (empty($cliargs[$longname]))
{
print "Unknown argument '$longname'\n";
cliargs_print_usage_and_exit($cliargs);
}
$arginfo = $cliargs[$longname];
$argtype = $arginfo['type'];
if ($argtype==='switch')
{
$value = true;
}
else if (isset($argparts[1]))
{
$value = $argparts[1];
}
else if (($index+1)<$argc)
{
$value = $argv[$index+1];
$index += 1;
}
else
{
print "Missing value after '$longname'\n";
cliargs_print_usage_and_exit($cliargs);
}
$options[$longname] = $value;
}
}
foreach ($cliargs as $longname => $arginfo)
{
$type = $arginfo['type'];
if (!isset($options[$longname]))
{
if ($type=='required')
{
print("Missing required value for '$longname'\n");
cliargs_print_usage_and_exit($cliargs);
}
else if ($type=='optional')
{
if (!isset($arginfo['default']))
die('Missing default value for '.$long);
$options[$longname] = $arginfo['default'];
}
else if ($type=='switch')
{
$options[$longname] = false;
}
}
}
return $options;
}
?>