Browse Source

Fix type error in help generation

Fix type error cause by putting numbers into `strlen()`. When an optino
has a default value that is an integer/float generating help should
continue to work.

Fixes #13225
Mark Story 7 years ago
parent
commit
5fb9690056
2 changed files with 12 additions and 5 deletions
  1. 1 1
      src/Console/ConsoleInputOption.php
  2. 11 4
      tests/TestCase/Console/HelpFormatterTest.php

+ 1 - 1
src/Console/ConsoleInputOption.php

@@ -173,7 +173,7 @@ class ConsoleInputOption
     {
         $name = strlen($this->_short) > 0 ? ('-' . $this->_short) : ('--' . $this->_name);
         $default = '';
-        if (strlen($this->_default) > 0 && $this->_default !== true) {
+        if (strlen((string)$this->_default) > 0 && $this->_default !== true) {
             $default = ' ' . $this->_default;
         }
         if ($this->_choices) {

+ 11 - 4
tests/TestCase/Console/HelpFormatterTest.php

@@ -191,21 +191,28 @@ txt;
     {
         $parser = new ConsoleOptionParser('mycommand', false);
         $parser->addOption('test', ['help' => 'A test option.'])
-            ->addOption('connection', [
-                'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default',
-            ]);
+               ->addOption('number', [
+                   'help' => 'The number',
+                   'default' => 2,
+               ])
+                ->addOption('connection', [
+                    'short' => 'c',
+                    'help' => 'The connection to use.',
+                    'default' => 'default',
+                ]);
 
         $formatter = new HelpFormatter($parser);
         $result = $formatter->text();
         $expected = <<<txt
 <info>Usage:</info>
-cake mycommand [-c default] [-h] [--test]
+cake mycommand [-c default] [-h] [--number 2] [--test]
 
 <info>Options:</info>
 
 --connection, -c  The connection to use. <comment>(default:
                   default)</comment>
 --help, -h        Display this help.
+--number          The number <comment>(default: 2)</comment>
 --test            A test option.
 
 txt;