Last active
October 8, 2015 05:18
-
-
Save coderofsalvation/3283715 to your computer and use it in GitHub Desktop.
PHP CLI-cmd (minimal)
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/env php | |
<?php | |
/* | |
* cli skeleton - starting point for cli-command | |
* (needs chmod 755 for cli) | |
* (MINIMAL VERSION) | |
*/ | |
$args = array(); | |
$require = array('gd','simplexml'); | |
function main($argv) { | |
global $args,$require; | |
if( count($argv) == 1 ) die(usage()); | |
$args = $argv; | |
init($argv); | |
print_r($args); | |
printf("[x] done\n"); | |
} | |
function init(){ | |
global $args; | |
$str = false; | |
$reqs = array("arg1","foo","bar"); | |
$opts = getopt(null, array('min:', 'max:','manual::')); | |
$args['parsed'] = array(); | |
if( isset($opts['manual']) ) manual(); | |
foreach( $args as $arg ) | |
if( is_string($arg) && !strstr( $arg, '--' ) && !strstr( $arg, basename(__FILE__) ) && $args['parsed'][ $reqs[0] ] = $arg ) | |
array_shift($reqs); | |
$args['parsed']['str'] = $str; | |
$args['parsed']['min'] = (int) isset($opts['min']) ? trim($opts['min']) : false; | |
$args['parsed']['max'] = (int) isset($opts['max']) ? trim($opts['max']) : 100; | |
} | |
function usage(){ | |
global $argv; | |
return "Usage: ./".basename($argv[0])." [--min=integer, --max=integer] <arg1> <foo> <bar>\n\n"; | |
} | |
if ('cli' === php_sapi_name() && basename(__FILE__) === basename($argv[0])) { | |
main($argv); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great starting point!
Feel free to visit my skeleton-version of your skeleton :)
badass: https://gist.github.com/3283636
minimal: https://gist.github.com/3283715