From cb5ce994fbeb1a9bb2f8261b785780d5a5633728 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sun, 29 Mar 2026 22:00:41 +0800 Subject: [PATCH] feat: add support for options with attached values by `=` --- system/CLI/CommandLineParser.php | 4 +++- tests/system/CLI/CommandLineParserTest.php | 18 ++++++++++++++++++ user_guide_src/source/changelogs/v4.8.0.rst | 4 ++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/system/CLI/CommandLineParser.php b/system/CLI/CommandLineParser.php index 25d944108705..e78ee6229c10 100644 --- a/system/CLI/CommandLineParser.php +++ b/system/CLI/CommandLineParser.php @@ -83,7 +83,9 @@ private function parseTokens(array $tokens): void $name = ltrim($token, '-'); $value = null; - if (isset($tokens[$index + 1]) && ! str_starts_with($tokens[$index + 1], '-')) { + if (str_contains($name, '=')) { + [$name, $value] = explode('=', $name, 2); + } elseif (isset($tokens[$index + 1]) && ! str_starts_with($tokens[$index + 1], '-')) { $value = $tokens[$index + 1]; $optionValue = true; diff --git a/tests/system/CLI/CommandLineParserTest.php b/tests/system/CLI/CommandLineParserTest.php index f5fba66782a9..4bd80b423545 100644 --- a/tests/system/CLI/CommandLineParserTest.php +++ b/tests/system/CLI/CommandLineParserTest.php @@ -107,5 +107,23 @@ public static function provideParseCommand(): iterable ['b', 'c', 'd'], [], ]; + + yield 'options with equals sign' => [ + ['--key=value', '--foo='], + [], + ['key' => 'value', 'foo' => ''], + ]; + + yield 'options with equals sign and double hyphen' => [ + ['--key=value', '--foo=', 'bar', '--', 'b', 'c', 'd'], + ['bar', 'b', 'c', 'd'], + ['key' => 'value', 'foo' => ''], + ]; + + yield 'mixed options with and without equals sign' => [ + ['--key=value', '--foo', 'bar', '--', 'b', 'c', 'd'], + ['b', 'c', 'd'], + ['key' => 'value', 'foo' => 'bar'], + ]; } } diff --git a/user_guide_src/source/changelogs/v4.8.0.rst b/user_guide_src/source/changelogs/v4.8.0.rst index 2d027886e922..80c39f3aae6c 100644 --- a/user_guide_src/source/changelogs/v4.8.0.rst +++ b/user_guide_src/source/changelogs/v4.8.0.rst @@ -145,6 +145,8 @@ Commands - ``CLI`` now supports the ``--`` separator to mean that what follows are arguments, not options. This allows you to have arguments that start with ``-`` without them being treated as options. For example: ``spark my:command -- --myarg`` will pass ``--myarg`` as an argument instead of an option. +- ``CLI`` now supports options with values specified using an equals sign (e.g., ``--option=value``) in addition to the existing space-separated syntax (e.g., ``--option value``). + This provides more flexibility in how you can pass options to commands. Testing ======= @@ -197,6 +199,8 @@ HTTP Consequently, ``CURLRequest``'s ``$config`` parameter is unused and will be removed in a future release. - ``CLIRequest`` now supports the ``--`` separator to mean that what follows are arguments, not options. This allows you to have arguments that start with ``-`` without them being treated as options. For example: ``php index.php command -- --myarg`` will pass ``--myarg`` as an argument instead of an option. +- ``CLIRequest`` now supports options with values specified using an equals sign (e.g., ``--option=value``) in addition to the existing space-separated syntax (e.g., ``--option value``). + This provides more flexibility in how you can pass options to CLI requests. Validation ==========