Return to Snippet

Revision: 65501
at December 9, 2013 06:02 by pachanka


Updated Code
<?php
function parse_args($args) {
    $out = array();
    $last_arg = null;
    if(is_string($args)){
        $args = str_replace(array('=', "\'", '\"'), array('= ', '&#39;', '&#34;'), $args);
        $args = str_getcsv($args, ' ', '"');
        $tmp = array();
        foreach($args as $arg){
            if(!empty($arg) && $arg != "&#39;" && $arg != "=" && $arg != " "){
                $tmp[] = str_replace(array('= ', '&#39;', '&#34;'), array('=', "'", '"'), trim($arg));
            }
        }
        $args = $tmp;
    }
    for($i = 0, $il = sizeof($args); $i < $il; $i++){
        if( (bool)preg_match("/^--(.+)/", $args[$i], $match) ){
            $parts = explode("=", $match[1]);
            $key = preg_replace("/[^a-zA-Z0-9-]+/", "", $parts[0]);
            if(isset($args[$i+1]) && substr($args[$i],0,2) == '--'){
                $out[$key] = $args[$i+1];
                $i++;
            }else if(isset($parts[1])){
                $out[$key] = $parts[1];
            }else{
                $out[$key] = true;
            }            
            $last_arg = $key;
        }else if( (bool)preg_match("/^-([a-zA-Z0-9]+)/", $args[$i], $match) ){
            $len = strlen($match[1]);
            for( $j = 0, $jl = $len; $j < $jl; $j++ ){
                $key = $match[1]{$j};
                $val = ($args[$i+1]) ? $args[$i+1]: true;
                $out[$key] = ($match[0]{$len} == $match[1]{$j}) ? $val : true;
            }
            $last_arg = $key;
        }else if((bool) preg_match("/^([a-zA-Z0-9-]+)$/", $args[$i], $match) ){
            $key = $match[0];
            $out[$key] = true;
            $last_arg = $key;
        }else if($last_arg !== null) {
            $out[$last_arg] = $args[$i];
        }
    }
    return $out;
}

$str = 'yankee -D "oo\"d l e\'s" -went "2 town 2 buy him-self" -a pony --calledit=" \"macaroonis\' "';
var_dump(parse_args($str));
/* Will output:
array(9) {
  ["yankee"]=>
  bool(true)
  ["D"]=>
  string(10) "oo"d l e's"
  ["w"]=>
  bool(true)
  ["e"]=>
  bool(true)
  ["n"]=>
  bool(true)
  ["t"]=>
  string(21) "2 town 2 buy him-self"
  ["a"]=>
  string(4) "pony"
  ["pony"]=>
  bool(true)
  ["calledit"]=>
  string(12) ""macaroonis'"
}
*/

Revision: 65500
at December 9, 2013 05:57 by pachanka


Initial Code
<?php
function parse_args($args) {
    $out = array();
    $last_arg = null;
    if(is_string($args)){
        $args = str_replace(array('=', "\'", '\"'), array('= ', ''', '"'), $args);
        $args = str_getcsv($args, ' ', '"');
        $tmp = array();
        foreach($args as $arg){
            if(!empty($arg) && $arg != "'" && $arg != "=" && $arg != " "){
                $tmp[] = str_replace(array('= ', ''', '"'), array('=', "'", '"'), trim($arg));
            }
        }
        $args = $tmp;
    }
    for($i = 0, $il = sizeof($args); $i < $il; $i++){
        if( (bool)preg_match("/^--(.+)/", $args[$i], $match) ){
            $parts = explode("=", $match[1]);
            $key = preg_replace("/[^a-zA-Z0-9-]+/", "", $parts[0]);
            if(isset($args[$i+1]) && substr($args[$i],0,2) == '--'){
                $out[$key] = $args[$i+1];
                $i++;
            }else if(isset($parts[1])){
                $out[$key] = $parts[1];
            }else{
                $out[$key] = true;
            }            
            $last_arg = $key;
        }else if( (bool)preg_match("/^-([a-zA-Z0-9]+)/", $args[$i], $match) ){
            $len = strlen($match[1]);
            for( $j = 0, $jl = $len; $j < $jl; $j++ ){
                $key = $match[1]{$j};
                $val = ($args[$i+1]) ? $args[$i+1]: true;
                $out[$key] = ($match[0]{$len} == $match[1]{$j}) ? $val : true;
            }
            $last_arg = $key;
        }else if((bool) preg_match("/^([a-zA-Z0-9-]+)$/", $args[$i], $match) ){
            $key = $match[0];
            $out[$key] = true;
            $last_arg = $key;
        }else if($last_arg !== null) {
            $out[$last_arg] = $args[$i];
        }
    }
    return $out;
}

$str = 'yankee -D "oo\"d l e\'s" -went "2 town 2 buy him-self" -a pony --calledit=" \"macaroonis\' "';
var_dump(parse_args($str));
/* Will output:
array(9) {
  ["yankee"]=>
  bool(true)
  ["D"]=>
  string(10) "oo"d l e's"
  ["w"]=>
  bool(true)
  ["e"]=>
  bool(true)
  ["n"]=>
  bool(true)
  ["t"]=>
  string(21) "2 town 2 buy him-self"
  ["a"]=>
  string(4) "pony"
  ["pony"]=>
  bool(true)
  ["calledit"]=>
  string(12) ""macaroonis'"
}
*/

Initial URL
http://snipplr.com/view/6677/

Initial Description
As seen in [maid450](http://snipplr.com/view/6677/)'s profile, added the ability to parse strings, and the ability to parse quoted strings, also the ability to regognize second arguments after "--" flags.

Initial Title
PHP argument parser

Initial Tags


Initial Language
PHP