<?
/*
GetOpt implementation for Subutux CLI
-------------------------------------

@author subutux <subutux@gmail.com>
@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;
}
?>