Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active October 8, 2015 05:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderofsalvation/3283715 to your computer and use it in GitHub Desktop.
Save coderofsalvation/3283715 to your computer and use it in GitHub Desktop.
PHP CLI-cmd (minimal)
#!/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);
}
?>
@coderofsalvation
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment