From 8fbe7d37e1dbebe27f598e80f2d1ef48c7ad31bb Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Fri, 4 Dec 2026 13:59:50 -0600 Subject: [PATCH] [#68] - Smaller handleArguments() with one handler per argument type Assisted-by: Claude Code --- src/Parser.php | 119 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 77 insertions(+), 42 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index 75c1d43..9078bba 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -174,53 +174,88 @@ private function getParamWithEqual(string $arg, int $eqPos): array */ private function handleArguments(array $argv): array { - $count = count($argv); - for ($i = 0, $j = $count; $i < $j; $i++) { - // --foo --bar=baz - if (str_starts_with($argv[$i], '--')) { - if ($this->parseAndMergeCommandWithEqualSign($argv[$i])) {// --bar=baz - continue; - } - - $key = $this->stripSlashes($argv[$i]); - if (!$this->isFlag($key) && $i + 1 < $j && !str_starts_with($argv[$i + 1], '-')) {// --foo value - $this->parsedCommands[$key] = $argv[$i + 1]; - $i++; - continue; - } - $this->parsedCommands[$key] = $this->parsedCommands[$key] ?? true; // --foo - continue; - } - - // -k=value -abc - if (str_starts_with($argv[$i], '-')) { - if ($this->parseAndMergeCommandWithEqualSign($argv[$i])) {// -k=value - continue; - } - - // -a value1 -abc value2 -abc - // A flag letter is true. The other letters take the value. - $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) { - $this->parsedCommands[$char] = $takesValue && !$this->isFlag($char) - ? $argv[$i + 1] - : true; - } - - if ($takesValue) {// -a value1 -abc value2 - $i++; - } - continue; - } - - $this->parsedCommands[] = $argv[$i]; + $count = count($argv); + $position = 0; + while ($position < $count) { + $argument = $argv[$position]; + $next = $argv[$position + 1] ?? null; + $value = $next === null || str_starts_with($next, '-') ? null : $next; + + $position += match (true) { + str_starts_with($argument, '--') => $this->handleLongOption($argument, $value), + str_starts_with($argument, '-') => $this->handleShortOptions($argument, $value), + default => $this->handlePositional($argument), + }; } return $this->parsedCommands; } + /** + * Handle a long option: --foo, --foo value, --foo=bar + * + * @param string $argument The option + * @param string|null $value The next argument, if it is not an option + * + * @return int The number of arguments used + */ + private function handleLongOption(string $argument, ?string $value): int + { + if ($this->parseAndMergeCommandWithEqualSign($argument)) {// --foo=bar + return 1; + } + + $key = $this->stripSlashes($argument); + if ($value !== null && !$this->isFlag($key)) {// --foo value + $this->parsedCommands[$key] = $value; + + return 2; + } + + $this->parsedCommands[$key] = $this->parsedCommands[$key] ?? true; // --foo + + return 1; + } + + /** + * Handle a positional argument + * + * @param string $argument The argument + * + * @return int The number of arguments used + */ + private function handlePositional(string $argument): int + { + $this->parsedCommands[] = $argument; + + return 1; + } + + /** + * Handle short options: -k=value, -abc, -abc value + * + * A flag letter is true. The other letters take the value. + * + * @param string $argument The options + * @param string|null $value The next argument, if it is not an option + * + * @return int The number of arguments used + */ + private function handleShortOptions(string $argument, ?string $value): int + { + if ($this->parseAndMergeCommandWithEqualSign($argument)) {// -k=value + return 1; + } + + $letters = str_split(substr($argument, 1)); + $takesValue = $value !== null && array_diff($letters, $this->flags) !== []; + foreach ($letters as $letter) { + $this->parsedCommands[$letter] = $takesValue && !$this->isFlag($letter) ? $value : true; + } + + return $takesValue ? 2 : 1; + } + /** * Check if an option takes no value. *