From 8ebb4c9feb08cda01f5b63b63e52c532b07621d9 Mon Sep 17 00:00:00 2001 From: Thomas Flori <thflori@gmail.com> Date: Tue, 15 May 2018 08:53:12 +0200 Subject: [PATCH] fix infinite loop with long words in descriptions This solves #127 --- src/Help.php | 11 ++++++++++- test/Options/HelpTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Help.php b/src/Help.php index 04dab40..01708f3 100644 --- a/src/Help.php +++ b/src/Help.php @@ -319,7 +319,16 @@ class Help implements HelpInterface while (mb_strlen($row) > $screenWidth) { $p = strrpos(substr($row, 0, $screenWidth), ' '); - $text .= substr($row, 0, $p) . PHP_EOL; + if ($p < $columnWidth+4) { + // no space - check for dash + $p = strrpos(substr($row, 0, $screenWidth), '-'); + if ($p < $columnWidth+4) { + // break at screen width + $p = $screenWidth-1; + } + } + $c = substr($row, $p, 1); + $text .= substr($row, 0, $p) . ($c !== ' ' ? $c : '') . PHP_EOL; $row = sprintf(' %s %s', str_repeat(' ', $columnWidth), substr($row, $p+1)); } diff --git a/test/Options/HelpTest.php b/test/Options/HelpTest.php index 55a58a3..1f2e0cd 100644 --- a/test/Options/HelpTest.php +++ b/test/Options/HelpTest.php @@ -3,6 +3,7 @@ namespace GetOpt\Test\Options; use GetOpt\GetOpt; +use GetOpt\Help; use GetOpt\Option; use PHPUnit\Framework\TestCase; @@ -77,6 +78,32 @@ class HelpTest extends TestCase ); } + /** @test */ + public function longWordsInDescription() + { + defined('COLUMNS') || define('COLUMNS', 90); + + $getopt = new GetOpt([ + ['a', 'alpha', GetOpt::OPTIONAL_ARGUMENT, 'This-is-a-long-word-with-dashes-what-should-not-cause-an-issue'], + ['b', 'beta', GetOpt::OPTIONAL_ARGUMENT, 'ThisWillCauseAnIssueBecauseWeDontKnowWhereToBreakInThisLongWord'], + ]); + + $script = $_SERVER['PHP_SELF']; + self::assertSame( + 'Usage: ' . $script . ' [options] [operands]' . PHP_EOL . PHP_EOL . + 'Options:' . PHP_EOL . + ' -a, --alpha [<arg>] This-is-a-long-' . PHP_EOL . + ' word-with-dashes-' . PHP_EOL . + ' what-should-not-' . PHP_EOL . + ' cause-an-issue' . PHP_EOL . + ' -b, --beta [<arg>] ThisWillCauseAnIs' . PHP_EOL . + ' sueBecauseWeDontK' . PHP_EOL . + ' nowWhereToBreakIn' . PHP_EOL . + ' ThisLongWord' . PHP_EOL . PHP_EOL, + $getopt->getHelpText([Help::MAX_WIDTH => 40]) + ); + } + /** @test */ public function helpTextWithArgumentName() { -- GitLab