Skip to content

Instantly share code, notes, and snippets.

@m6w6
Created April 10, 2019 15:02
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 m6w6/8efca204f26714bbc932b661fad62c86 to your computer and use it in GitHub Desktop.
Save m6w6/8efca204f26714bbc932b661fad62c86 to your computer and use it in GitHub Desktop.
command not found handler
ask_php () {
typeset res ret
php -derror_reporting=-1 -dlog_errors=0 -ddisplay_errors=1 -r'
set_error_handler(function($code, $message) {
fprintf(STDERR, "%s\n", $message);
exit(1);
});
array_shift($argv);
$fn = array_shift($argv);
if (!function_exists($fn)) {
exit(127);
}
foreach ($argv as &$argp) {
if ($argp === "-") {
$argp = stream_get_contents(STDIN);
} elseif (defined($argp)) {
$argp = constant($argp);
}
}
$res = call_user_func_array($fn, $argv);
if (is_bool($res)) {
exit(intval(!$res));
} elseif (is_scalar($res)) {
printf("%s\n", $res);
} elseif (isset($res)) {
var_export($res);
print("\n");
}
' -- "$@"
ret=$?
if test $ret -eq 127
then
echo $SHELL_NAME: command not found: $1 >&2
fi
return $ret
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment