diff --git a/.travis.yml b/.travis.yml index 2c8141b..5ef6eb7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,5 +21,7 @@ before_script: script: - composer code-style - - vendor/bin/phpunit -c phpunit.xml --coverage-clover=build/logs/clover.xml --coverage-text - - sh -c 'if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then php vendor/bin/coveralls -v; fi;' + - sh -c 'if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then composer coverage; else composer test; fi;' + +after_script: + - sh -c 'if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then php vendor/bin/coveralls -v -x build/coverage.xml; fi;' diff --git a/composer.json b/composer.json index db536aa..1994a45 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ } ], "require": { - "php": ">=5.4.0" + "php": ">=5.4.0", + "ext-mbstring": "*" }, "require-dev": { "phpunit/phpunit": "^4.8", @@ -32,6 +33,8 @@ } }, "scripts": { - "code-style": "phpcs --standard=PSR2 src && phpcs --standard=PSR2 test" + "code-style": "vendor/bin/phpcs --standard=PSR2 src && vendor/bin/phpcs --standard=PSR2 test", + "test": "vendor/bin/phpunit -c phpunit.xml", + "coverage": "vendor/bin/phpunit -c phpunit.xml --coverage-clover=build/coverage.xml --coverage-html=build/coverage --coverage-text" } } diff --git a/phpunit.xml b/phpunit.xml index 9c3d087..dc9954f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -21,15 +21,4 @@ - - - - diff --git a/src/GetOpt.php b/src/GetOpt.php index da653c2..0ec27c3 100644 --- a/src/GetOpt.php +++ b/src/GetOpt.php @@ -189,7 +189,7 @@ public function process($arguments = null) $arguments->process($this, $setOption, $setCommand, $addOperand); if (($operand = $this->nextOperand()) && $operand->isRequired() && - (!$operand->isMultiple() || count($this->getOperand($operand->getName())) === 0) + (!$operand->isMultiple() || count($operand->getValue()) === 0) ) { throw new Missing(sprintf('Operand %s is required', $operand->getName())); } diff --git a/src/Help.php b/src/Help.php index 2ba6bdd..bb8419c 100644 --- a/src/Help.php +++ b/src/Help.php @@ -325,7 +325,16 @@ protected function renderColumns($columnWidth, $data) 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/src/Operand.php b/src/Operand.php index e85d70a..b129878 100644 --- a/src/Operand.php +++ b/src/Operand.php @@ -115,7 +115,7 @@ public function getValue() } if ($this->isMultiple()) { - return $this->default !== null ? [ $this->default ] : null; + return $this->default !== null ? [ $this->default ] : []; } return $this->default; diff --git a/test/Operands/CommonTest.php b/test/Operands/CommonTest.php index 1f7d795..9a3916e 100644 --- a/test/Operands/CommonTest.php +++ b/test/Operands/CommonTest.php @@ -195,4 +195,19 @@ public function multipleFalse() self::assertFalse($operand->isMultiple()); } + + /** @test */ + public function requiredMultipleThrowsMissing() + { + $operand = new Operand('port'); + $operand->multiple(true); + $operand->required(true); + + $getOpt = new GetOpt(); + $getOpt->addOperand($operand); + + $this->setExpectedException('GetOpt\ArgumentException\Missing', 'Operand port is required'); + + $getOpt->process(''); + } } diff --git a/test/Options/HelpTest.php b/test/Options/HelpTest.php index e737e3f..d375564 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 @@ public function helpTextWithLongDescriptions() ); } + /** @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 [] This-is-a-long-' . PHP_EOL . + ' word-with-dashes-' . PHP_EOL . + ' what-should-not-' . PHP_EOL . + ' cause-an-issue' . PHP_EOL . + ' -b, --beta [] ThisWillCauseAnIs' . PHP_EOL . + ' sueBecauseWeDontK' . PHP_EOL . + ' nowWhereToBreakIn' . PHP_EOL . + ' ThisLongWord' . PHP_EOL . PHP_EOL, + $getopt->getHelpText([Help::MAX_WIDTH => 40]) + ); + } + /** @test */ public function helpTextWithArgumentName() {