Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ All notable changes are documented here. The format is based on [Keep a Changelo

### Fixed

- An empty argument after an option (`--name ''` or `-n ''`) no longer gives the PHP warning `Uninitialized string offset 0`. The value is an empty string, as before. [#66](https://github.com/phalcon/cli-options-parser/issues/66)

### Removed

### Upgrade Notes
Expand Down
4 changes: 2 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private function handleArguments(array $argv): array
}

$key = $this->stripSlashes($argv[$i]);
if (!$this->isFlag($key) && $i + 1 < $j && $argv[$i + 1][0] !== '-') {// --foo value
if (!$this->isFlag($key) && $i + 1 < $j && !str_starts_with($argv[$i + 1], '-')) {// --foo value
$this->parsedCommands[$key] = $argv[$i + 1];
$i++;
continue;
Expand All @@ -200,7 +200,7 @@ private function handleArguments(array $argv): array

// -a value1 -abc value2 -abc
// A flag letter is true. The other letters take the value.
$hasNextElementDash = !($i + 1 < $j && $argv[$i + 1][0] !== '-');
$hasNextElementDash = !($i + 1 < $j && !str_starts_with($argv[$i + 1], '-'));
$chars = str_split(substr($argv[$i], 1));
$takesValue = !$hasNextElementDash && array_diff($chars, $this->flags) !== [];
foreach ($chars as $char) {
Expand Down
10 changes: 10 additions & 0 deletions tests/support/Traits/ParserDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public static function parseProvider(): array
'params' => ['command' => ['/usr/bin/phalcon', 'arg1', 'arg2', 'arg3']],
'expected' => [0 => 'arg1', 1 => 'arg2', 2 => 'arg3'],
],
// --name '': an empty value, with no warning
[
'params' => ['command' => ['/usr/bin/phalcon', '--name', '']],
'expected' => ['name' => ''],
],
// -n '': an empty value, with no warning
[
'params' => ['command' => ['/usr/bin/phalcon', '-n', '']],
'expected' => ['n' => ''],
],
// plain-arg --foo --bar=baz --funny=spam=eggs --also-funny=spam=eggs 'plain arg 2'
// -abc -k=value 'plain arg 3' --s=original --s=overwrite --s
[
Expand Down