PHP CLI arguments

Ever wanted to group parameters that you receive? Here’s a method:

function processArguments($groupDelimiter = '-'){
    global $argv;
    array_shift($argv);//remove the script name
    $delimiterLength = strlen($groupDelimiter);
    $args = array();
    $lastGroup = null;
    foreach($argv as $arg){
        if(substr($arg, 0, $delimiterLength) == $groupDelimiter){
            $lastGroup = substr($arg, $delimiterLength, strlen($arg)-$delimiterLength);
            $args[$lastGroup] = array();
        }else{
            if($lastGroup === null)
                $args[$arg] = $arg;
            else
                array_push($args[$lastGroup], $arg);
        }
    }
    return $args;
}

Result:

paul@samsung:/home/paul$ ./run.php free element -list item1 item2 ... -empty_list
Array
(
    [free] => free
    [element] => element
    [list] => Array
    (
        [0] => item1
        [1] => item2
        [2] => ...
    )

    [empty_list] => Array
    (
    )

)