<?php

namespace astropenguin\botanical;
use ArrayObject;

class Args
{
	//parseOptions utilitiese by tgckpg
	static function PARSE ( $argStream, $handler )
	{
		//Chop first useless argument -- argv[0]
		array_shift ( $argStream ) ;
		//Initiate ArrayObject for iterator
		$arrayobject = new ArrayObject ( $argStream ) ;
		//Initiate iterator for iteration
		$iterator = $arrayobject->getIterator();

		//If options is set first
		if( $iterator->valid() && preg_match ( '/^-\w$/', $iterator->current() ) )
		{
			//iterate through whole argument stream
			for (	; $iterator->valid(); $iterator->next() )
			{
				//Check if reached next option
				if( preg_match ( '/^-\w$/', $opts = $iterator->current() ) )
				{
					//Get current options
					$currOpt = $opts;
					//echo "$currOpt\n";
					//Test if next stream is an option
					for ( $iterator->next(); $iterator->valid(); $iterator->next() )
					{
						if ( preg_match ( '/^-\w$/', $opts = $iterator->current() ) ) 
						{
							//echo "$currOpt $opts\n";
							$handler($currOpt);
							$currOpt = $opts;
						} else break;
						//var_dump($iterator->valid());
					}
				}//End if
				//echo "$currOpt $opts\n";
				$handler($currOpt, $opts);
				//A temporary fix for infinite loop
				if(!$iterator->valid()) break;
			}// End for
		}
		// If option is not set first.
		else
		{
			//Try other approach.
		}
		// End if
	}
}