Created
August 13, 2012 17:25
-
-
Save ichiriac/3342610 to your computer and use it in GitHub Desktop.
Defines an alternative function for *HANDY* getopts PHP native function ....
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Reads automatically arguments options | |
* @return array | |
*/ | |
function get_opts() { | |
$result = array(); | |
$key = null; | |
$value = null; | |
$asize = count($_SERVER['argv']); | |
for( $i = 1; $i < $asize; $i++ ) { | |
$arg = $_SERVER['argv'][$i]; | |
if ( $arg[0] === '-' ) { | |
if ( $key ) { | |
$result[ $key ] = is_null($value) ? false : $value; | |
} | |
$key = ltrim( $arg, '-' ); | |
$value = null; | |
} else { | |
if ( $key ) { | |
if ( !$value ) { | |
$value = $arg; | |
} else { | |
if ( !is_array($value) ) { | |
$value = array($value); | |
} | |
$value[] = $arg; | |
} | |
} else { | |
$result[] = $arg; | |
} | |
} | |
} | |
if ( $key ) { | |
$result[ $key ] = is_null($value) ? false : $value; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment