Browse Source

Fix required options obstructing the usage of `--help`.

This allows to invoke help when required options are configured, just
like it's already possible for required arguments.
ndm2 4 years ago
parent
commit
02b6f3b2ed

+ 6 - 1
src/Console/ConsoleOptionParser.php

@@ -705,8 +705,13 @@ class ConsoleOptionParser
                 $args = $this->_parseArg($token, $args);
             }
         }
+
+        if (isset($params['help'])) {
+            return [$params, $args];
+        }
+
         foreach ($this->_args as $i => $arg) {
-            if ($arg->isRequired() && !isset($args[$i]) && empty($params['help'])) {
+            if ($arg->isRequired() && !isset($args[$i])) {
                 throw new ConsoleException(
                     sprintf('Missing required argument. The `%s` argument is required.', $arg->name())
                 );

+ 15 - 2
tests/TestCase/Console/ConsoleOptionParserTest.php

@@ -695,9 +695,9 @@ class ConsoleOptionParserTest extends TestCase
     }
 
     /**
-     * test that no exception is triggered when help is being generated
+     * test that no exception is triggered for required arguments when help is being generated
      */
-    public function testHelpNoExceptionWhenGettingHelp(): void
+    public function testHelpNoExceptionForRequiredArgumentsWhenGettingHelp(): void
     {
         $parser = new ConsoleOptionParser('mycommand', false);
         $parser->addOption('test', ['help' => 'A test option.'])
@@ -708,6 +708,19 @@ class ConsoleOptionParserTest extends TestCase
     }
 
     /**
+     * test that no exception is triggered for required options when help is being generated
+     */
+    public function testHelpNoExceptionForRequiredOptionsWhenGettingHelp(): void
+    {
+        $parser = new ConsoleOptionParser('mycommand', false);
+        $parser->addOption('test', ['help' => 'A test option.'])
+            ->addOption('model', ['help' => 'The model to make.', 'required' => true]);
+
+        $result = $parser->parse(['--help']);
+        $this->assertTrue($result[0]['help']);
+    }
+
+    /**
      * test that help() with a command param shows the help for a subcommand
      */
     public function testHelpSubcommandHelp(): void