Created
January 23, 2015 09:28
-
-
Save msankhala/ebcc8e4d3f5f2cf0cb48 to your computer and use it in GitHub Desktop.
php script arguments.
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
#!/usr/bin/php | |
<?php | |
print_r(arguments($argv)); | |
function arguments($args) { | |
$ret = array( | |
'exec' => '', | |
'options' => array(), | |
'flags' => array(), | |
'arguments' => array(), | |
); | |
$ret['exec'] = array_shift($args); | |
while (($arg = array_shift($args)) != NULL) { | |
// Is it a option? (prefixed with --). | |
if (substr($arg, 0, 2) === '--') { | |
$option = substr($arg, 2); | |
// Is it the syntax '--option=argument'?. | |
if (strpos($option, '=') !== FALSE) { | |
array_push($ret['options'], explode('=', $option, 2)); | |
} | |
else { | |
array_push($ret['options'], $option); | |
} | |
continue; | |
} | |
// Is it a flag or a serial of flags? (prefixed with -). | |
if (substr($arg, 0, 1) === '-') { | |
for ($i = 1; isset($arg[$i]); $i++) { | |
$ret['flags'][] = $arg[$i]; | |
} | |
continue; | |
} | |
// Finally, it is not option, nor flag. | |
$ret['arguments'][] = $arg; | |
continue; | |
} | |
return $ret; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment