Skip to content

Instantly share code, notes, and snippets.

@ichiriac
Created August 13, 2012 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ichiriac/3342610 to your computer and use it in GitHub Desktop.
Save ichiriac/3342610 to your computer and use it in GitHub Desktop.
Defines an alternative function for *HANDY* getopts PHP native function ....
<?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