From 4e25fe5d0749b17d316157dd0598d81beff8dd35 Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Fri, 4 Dec 2026 13:58:09 -0600 Subject: [PATCH] [#66] - No warning for an empty argument after an option Assisted-by: Claude Code --- CHANGELOG.md | 2 ++ src/Parser.php | 4 ++-- tests/support/Traits/ParserDataTrait.php | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 725e376..45aeea8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Parser.php b/src/Parser.php index 77451e3..75c1d43 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -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; @@ -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) { diff --git a/tests/support/Traits/ParserDataTrait.php b/tests/support/Traits/ParserDataTrait.php index 8ac64e1..bb8ca9b 100644 --- a/tests/support/Traits/ParserDataTrait.php +++ b/tests/support/Traits/ParserDataTrait.php @@ -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 [