Created
April 10, 2019 15:02
-
-
Save m6w6/8efca204f26714bbc932b661fad62c86 to your computer and use it in GitHub Desktop.
command not found handler
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
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