getoptarray
Code (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
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
<?
/*
GetOpt implementation for Subutux CLI
-------------------------------------
@author subutux <[email protected]>
@func getopt_array($agruments)
@edit 06/01/2010
usage:
$agruments = "--test 22 -gogo=gadget -wonderwall foo-bar -yezz";
$opts = getopt_array($agruments);
echo "<pre>";
print_r($opts);
echo "</pre>";
output:
Array
(
[--test] => 22
[-gogo] => gadget
[-wonderwall] => foo-bar
[-yezz] => 1
)
*/
function getopt_array($agrs){
$opts = array();
$agrs = preg_split("/\s+/",$agrs);
// ik gebruik hier i.p.v. de explode functie, preg_split omdat, als er meerdere spaties zijn,
// deze als lege waardes in de array $args komen te staan en zorgt voor (kleine maar toch) vertraging.
reset($agr);
while (list($key, $val) = each($agrs)) {
if (preg_match("/^-(.*)=(.*)/",$val)){
$opt = explode ("=",$val);
$opts[$opt[0]] = $opt[1];
}
elseif (preg_match("/^-/",$val)){
$next_agr = $agrs[$key+1];
if (!preg_match("/^-/",$next_agr)){
$opts[$val] = $next_agr;
} else {
$opts[$val] = true;
}
}
}
return $opts;
}
?>
/*
GetOpt implementation for Subutux CLI
-------------------------------------
@author subutux <[email protected]>
@func getopt_array($agruments)
@edit 06/01/2010
usage:
$agruments = "--test 22 -gogo=gadget -wonderwall foo-bar -yezz";
$opts = getopt_array($agruments);
echo "<pre>";
print_r($opts);
echo "</pre>";
output:
Array
(
[--test] => 22
[-gogo] => gadget
[-wonderwall] => foo-bar
[-yezz] => 1
)
*/
function getopt_array($agrs){
$opts = array();
$agrs = preg_split("/\s+/",$agrs);
// ik gebruik hier i.p.v. de explode functie, preg_split omdat, als er meerdere spaties zijn,
// deze als lege waardes in de array $args komen te staan en zorgt voor (kleine maar toch) vertraging.
reset($agr);
while (list($key, $val) = each($agrs)) {
if (preg_match("/^-(.*)=(.*)/",$val)){
$opt = explode ("=",$val);
$opts[$opt[0]] = $opt[1];
}
elseif (preg_match("/^-/",$val)){
$next_agr = $agrs[$key+1];
if (!preg_match("/^-/",$next_agr)){
$opts[$val] = $next_agr;
} else {
$opts[$val] = true;
}
}
}
return $opts;
}
?>