Skip to content

Commit be8b71f

Browse files
committed
Cli: improve option parsing
1 parent 0fc0b26 commit be8b71f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Cli.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public function __construct(string $cwd, array $argv)
9898
$this->providedOptions[$optionName] = $optionArgument;
9999
}
100100
} else {
101+
if ($this->getOptionArgumentAfterAssign($arg) !== null) {
102+
throw new InvalidCliException("Option --$optionName does not accept arguments, see --help");
103+
}
104+
101105
$this->providedOptions[$optionName] = true;
102106
}
103107
}
@@ -122,7 +126,10 @@ private function isOptionWithRequiredValue(string $optionName): bool
122126
private function getKnownOptionName(string $option): ?string
123127
{
124128
foreach (self::OPTIONS as $knownOption => $needsArgument) {
125-
if (strpos($option, $knownOption) === 0) {
129+
if (
130+
strpos($option, $knownOption) === 0
131+
&& (strlen($option) === strlen($knownOption) || $option[strlen($knownOption)] === '=')
132+
) {
126133
return $knownOption;
127134
}
128135
}

tests/CliTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ public function validationDataProvider(): iterable
115115
['bin/script.php', '--composer-json='],
116116
];
117117

118+
yield 'valid option is substring of provided option' => [
119+
'Unknown option --configuration=foo, see --help',
120+
['bin/script.php', '--configuration=foo'],
121+
];
122+
123+
yield 'argument-less option with argument' => [
124+
'Option --verbose does not accept arguments, see --help',
125+
['bin/script.php', '--verbose=foo'],
126+
];
127+
118128
yield 'suggestion #1' => [
119129
'Unknown option --hep, did you mean --help?',
120130
['bin/script.php', '--hep'],

0 commit comments

Comments
 (0)