8000 CLI: Send error output to stderr #24 by maximkou · Pull Request #31 · cebe/markdown · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

CLI: Send error output to stderr #24 #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
< 8000 form data-turbo="false" action="/cebe/markdown/pull/31/diffview" accept-charset="UTF-8" method="post">
Diff view
39 changes: 25 additions & 14 deletions bin/markdown
8000
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require(__DIR__ . '/../Parser.php');
require(__DIR__ . '/../Markdown.php');

// Send all errors to stderr
ini_set('display_errors', 'stderr');

$flavor = 'cebe\\markdown\\Markdown';
$flavors = [
'gfm' => ['cebe\\markdown\\GithubMarkdown', __DIR__ . '/../GithubMarkdown.php'],
Expand All @@ -23,14 +26,10 @@ foreach($argv as $k => $arg) {
require($flavors[$arg[1]][1]);
$flavor = $flavors[$arg[1]][0];
} else {
echo "Error: Unknown flavor: " . $arg[1] . "\n";
usage();
exit(1);
error("Unknown flavor: " . $arg[1], "usage");
}
} else {
echo "Error: Incomplete argument --flavor!\n";
usage();
exit(1);
error("Incomplete argument --flavor!", "usage");
}
break;
case '-h':
Expand All @@ -41,9 +40,7 @@ foreach($argv as $k => $arg) {
usage();
break;
default:
echo "Error: Unknown argument " . $arg[0] . "\n";
usage();
exit(1);
error("Unknown argument " . $arg[0], "usage");
}
} else {
$src[] = $arg;
Expand All @@ -55,14 +52,11 @@ if (empty($src)) {
} elseif (count($src) == 1) {
$file = reset($src);
if (!file_exists($file)) {
echo "Error: File does not exist: $file\n";
exit(1);
error("File does not exist:" . $file);
}
$markdown = file_get_contents($file);
} else {
echo "Error: Converting multiple files is not yet supported.\n";
usage();
exit(1);
error("Error: Converting multiple files is not yet supported.", "usage");
}

/** @var cebe\markdown\Parser $md */
Expand Down Expand Up @@ -108,3 +102,20 @@ Examples:
EOF;
exit(1);
}

/**
* Send custom error message to stderr
* @param $message string
* @param $callback mixed called before script exit
* @return void
*/
function error($message, $callback = null) {
$fe = fopen("php://stderr", "w");
fwrite($fe, "Error: " . $message . "\n");

if (is_callable($callback)) {
call_user_func($callback);
}

exit(1);
}
0