Skip to content

Commit

Permalink
Write help to stderr if no Markdown is given
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Sep 19, 2015
1 parent ea2ba42 commit 1af805d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
22 changes: 9 additions & 13 deletions bin/commonmark
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ foreach ($argv as $i => $arg) {
switch ($arg) {
case '-h':
case '--help':
help();
break;
echo getHelpText();
exit(0);
default:
fail('Unknown option: ' . $arg);
}
Expand All @@ -37,19 +37,21 @@ if (isset($src)) {
fclose($stdin);

if (empty($markdown)) {
help();
fail(getHelpText());
}
}

$converter = new \League\CommonMark\CommonMarkConverter();
echo $converter->convertToHtml($markdown);

/**
* Display help and usage info
* Get help and usage info
*
* @return string
*/
function help()
function getHelpText()
{
echo <<<HELP
return <<<HELP
CommonMark - Markdown done right
Usage: commonmark [OPTIONS] [FILE]
Expand Down Expand Up @@ -79,20 +81,14 @@ Examples:
Full documentation can be found at http://commonmark.thephpleague.com/
HELP;

exit(0);
}

/**
* @param string $message Error message
*/
function fail($message)
{
$message .= "\n";

$fp = fopen('php://stderr', 'w');
fwrite($fp, $message);
fclose($fp);
fwrite(STDERR, $message . "\n");
exit(1);
}

Expand Down
5 changes: 3 additions & 2 deletions tests/functional/BinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public function testNoArgsOrStdin()
$cmd = new Command($this->getPathToCommonmark());
$cmd->execute();

$this->assertEquals(0, $cmd->getExitCode());
$this->assertContains('Usage:', $cmd->getOutput());
$this->assertEquals(1, $cmd->getExitCode());
$this->assertEmpty($cmd->getOutput());
$this->assertContains('Usage:', $cmd->getError());
}

/**
Expand Down

1 comment on commit 1af805d

@cebe
Copy link

@cebe cebe commented on 1af805d Sep 19, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.