Skip to content

Instantly share code, notes, and snippets.

@rakkang
Last active August 2, 2016 07:21
Show Gist options
  • Save rakkang/ed63111b3a03577f577e to your computer and use it in GitHub Desktop.
Save rakkang/ed63111b3a03577f577e to your computer and use it in GitHub Desktop.
A executable PHP script template.
<?php
/**
* @desc Shell execuatable php script
* @author rakkang
* @date 2015-12-30
*/
const G_DESCRIPTION = 'Sample script.';
//!!!! DON'T CHANGE VARIABLE NAME
$g_options = array(
'g' => array(
'req' => false,
'func' => 'generate',
'args' => array(
'p' => array(true, 'pid'),
),
'exam' => '-gp [pid] / -g -p [pid]',
'desc' => 'generate a new order with a special pid',
),
);
/******************* Write your function code below *******************/
/**
* @param array $args
*/
function generate(array $args)
{
echo "Generate new order\n";
echo var_dump($args);
}
/******************* Write your function code above *******************/
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!DON'T REWRITE ANY CODE OF BELOW!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$_script_argvs = '';
foreach ($g_options as $opt => $item) {
$_script_argvs .= $opt . '' . ($item['req'] ? ':' : '');
if (is_array($item['args'])) {
foreach ($item['args'] as $arg => $attr) {
$_script_argvs .= $arg . '' . ($attr[0] ? ':' : '');
}
}
}
$_got_argvs = getopt($_script_argvs . 'h', array("help"));
$_script_name = end(explode("/", array_shift($argv)));
$_execute_opt = $g_options[$_got_argvs[0]];
function help()
{
global $g_options, $_script_name;
echo "Usage: ~/orp/.../bin/(php|hhvm) $_script_name [options] ... [args] ...\n";
echo G_DESCRIPTION . "\n\n";
echo "SHORT OPTIONS:\n";
foreach ($g_options as $opt => $item) {
$desc = sprintf("%8s", '-' . $opt) . ' ' . $item['desc'] . "\n";
$desc .= sprintf('%8s', '') . ' ' . "eg: $_script_name " . $item['exam'];
echo $desc . "\n\n";
}
echo "OPTIONS ARGUMENTS:\n";
foreach ($g_options as $opt => $item) {
if (is_array($item['args'])) {
foreach ($item['args'] as $arg => $attr) {
$desc = sprintf("%8s", '-' . $arg) . ' ';
$desc .= $attr[1] . ($attr[0] ? ". Required value" : '') . " for " . $item['desc'] . "\n";
echo $desc;
}
echo "\n";
}
}
}
if (empty($_got_argvs)) {
echo "$_script_name unkown option!\n\n";
help();
} else {
$opts = array_keys($_got_argvs);
if ($opts[0] == 'h' || $opts[0] == 'help') {
echo "Show help document.\n\n";
help();
} else {
$cmd = $g_options[$opts[0]];
if (!is_array($cmd)) {
echo "$_script_name option illegal!\n\n";
help();
} else {
$cmd['func']($_got_argvs);
}
}
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment