Skip to content

Commit cb5ce99

Browse files
committed
feat: add support for options with attached values by =
1 parent 51deee5 commit cb5ce99

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

system/CLI/CommandLineParser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ private function parseTokens(array $tokens): void
8383
$name = ltrim($token, '-');
8484
$value = null;
8585

86-
if (isset($tokens[$index + 1]) && ! str_starts_with($tokens[$index + 1], '-')) {
86+
if (str_contains($name, '=')) {
87+
[$name, $value] = explode('=', $name, 2);
88+
} elseif (isset($tokens[$index + 1]) && ! str_starts_with($tokens[$index + 1], '-')) {
8789
$value = $tokens[$index + 1];
8890

8991
$optionValue = true;

tests/system/CLI/CommandLineParserTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,23 @@ public static function provideParseCommand(): iterable
107107
['b', 'c', 'd'],
108108
[],
109109
];
110+
111+
yield 'options with equals sign' => [
112+
['--key=value', '--foo='],
113+
[],
114+
['key' => 'value', 'foo' => ''],
115+
];
116+
117+
yield 'options with equals sign and double hyphen' => [
118+
['--key=value', '--foo=', 'bar', '--', 'b', 'c', 'd'],
119+
['bar', 'b', 'c', 'd'],
120+
['key' => 'value', 'foo' => ''],
121+
];
122+
123+
yield 'mixed options with and without equals sign' => [
124+
['--key=value', '--foo', 'bar', '--', 'b', 'c', 'd'],
125+
['b', 'c', 'd'],
126+
['key' => 'value', 'foo' => 'bar'],
127+
];
110128
}
111129
}

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ Commands
145145

146146
- ``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.
147147
For example: ``spark my:command -- --myarg`` will pass ``--myarg`` as an argument instead of an option.
148+
- ``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``).
149+
This provides more flexibility in how you can pass options to commands.
148150

149151
Testing
150152
=======
@@ -197,6 +199,8 @@ HTTP
197199
Consequently, ``CURLRequest``'s ``$config`` parameter is unused and will be removed in a future release.
198200
- ``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.
199201
For example: ``php index.php command -- --myarg`` will pass ``--myarg`` as an argument instead of an option.
202+
- ``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``).
203+
This provides more flexibility in how you can pass options to CLI requests.
200204

201205
Validation
202206
==========

0 commit comments

Comments
 (0)