Assembla home | Assembla project page
 

Changeset 147

Show
Ignore:
Timestamp:
03/09/08 10:00:20 (9 months ago)
Author:
Slynderdale
Message:

Added a parseArguments function that takes a given string and extracts all the commands like arguments from it then returns an array. See the function's comments for more information on it.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Phergie/Plugin/Abstract/Base.php

    r138 r147  
    278278    } 
    279279 
     280        /** 
     281        * Parses a string of command line like arguments and returns them as an array 
     282        * Parses the following: 
     283        *    -Strings: "" | "foo" | "foo bar" | "foo \" bar" | foo 
     284        *    -Options: --opt | --opt foo | --opt= | --opt=foo | --opt="" | --opt="foo" | --opt="foo bar" 
     285        *      @Note: If no parameter is given, the value returned by the option defaults to 
     286        *            1 unless its an empty value such as --opt="" or --opt= 
     287        *    -Flags: -flag +flag 
     288        *      @Note: Flags are handled using bitwise. 1 = -flag, 2 = +flag, 3 = Both a +flag and -flag 
     289        * 
     290        * The results get stored in an array for the following structure: 
     291        *     Array -> strings []                   -Array containing all the string matches 
     292        *     Array -> commands [command] => value  -Array containing all the string matches 
     293        *     Array -> flags [flag] => bitwise      -Array containing all the string matches 
     294        *     Array -> all [strings|commands|flags] -Array containing a list of all the 
     295        *                                            strings, commands and flags 
     296        * 
     297        * @param string $args String of arguments that will be parsed 
     298        * @return array 
     299        */ 
     300        public function parseArguments($args) { 
     301                // Strip away multiple spaces 
     302                $args = ' '.preg_replace('#\s+#', ' ', trim($args)).' '; 
     303 
     304                // Check the args string for agurments 
     305                preg_match_all('{'. 
     306                // String Regex: Matches "" | "foo" | "foo bar" | "foo \" bar" | foo 
     307                '(".*?(?<!\\\)" | [^-+\s]+) | '. 
     308                // Option Regex: Matches --opt | --opt foo | --opt= | --opt=foo | --opt="" | --opt="foo" | --opt="foo bar" 
     309                '(--\w+ (?:=".*?(?<!\\\)" | [=\s][^-+"\s]*)?) | '. 
     310                // Flag Regex: Matches -flag +flag 
     311                '([-+]\w+)'. 
     312                '}xi', 
     313                $args, $matches); 
     314                // Shifts the first group matches element off of matches 
     315                $matches = array_shift($matches); 
     316 
     317                // Returned data structure 
     318                $data = array( 
     319                    'strings'    => array(), 
     320                    'commands' => array(), 
     321                    'flags'    => array(), 
     322                    'all'      => array( 
     323                        'strings'    => '', 
     324                        'commands' => array(), 
     325                        'flags'    => array() 
     326                    ) 
     327                ); 
     328 
     329                // Loop through the results 
     330                foreach ($matches as $match) { 
     331                        $match = trim($match); 
     332                        // Check to see if the current match an an --option 
     333                        if (substr($match, 0, 2 ) === '--') { 
     334                                $value = preg_split('/[=\s]/', $match, 2); 
     335                                $com   = substr(array_shift($value), 2); 
     336                                $value = trim(join($value)); 
     337                                // Strip quotes from the option's value 
     338                                $realValue = (substr($value, 0, 1) === '"' && substr($value, -1) === '"' || substr($match, -1) === '='); 
     339                                if(substr($value, 0, 1) === '"' && substr($value, -1) === '"') { 
     340                                        $value = trim(substr($value, 1, -1)); 
     341                                } 
     342                                if (!in_array($com, $data['all']['commands'])) 
     343                                $data['all']['commands'][] = $com; 
     344                                $data['commands'][$com] = (!empty($value) || $realValue? str_replace('\"', '"', $value) : true); 
     345                                continue; 
     346                        } 
     347 
     348                        // Check to see if the current match is a -flag 
     349                        if (substr($match, 0, 1) === '-' || substr($match, 0, 1) === '+') { 
     350                                $flag = substr($match, 1); 
     351                                if (!in_array($flag, $data['all']['flags'])) 
     352                                $data['all']['flags'][] = $flag; 
     353                                // Handle the flags using bitwise operations. 1=-flag, 2=+flag, 3=Both a plus and minus 
     354                                $data['flags'][$flag] |= (substr($match, 0, 1) === '-' ? 0x1 : 0x2); 
     355                                continue; 
     356                        } 
     357 
     358                        // Strip the quotes away from match 
     359                        if(substr($match, 0, 1) === '"' && substr($match, -1) === '"') { 
     360                                $match = trim(substr($match, 1, -1)); 
     361                        } 
     362                        // The match value isn't a flag or an option so consider it a string 
     363                        if (!empty($match)) 
     364                        $data['strings'][] = str_replace('\"', '"', $match); 
     365                } 
     366 
     367                $data['all']['strings'] = trim(implode(' ', $data['strings'])); 
     368                return $data; 
     369        } 
     370 
    280371    /** 
    281372    * Returns whether or not the current environment meets the requirements