From da3739d3927f9b1852041be72e255741d340320c Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 29 Apr 2025 22:15:15 +0200 Subject: [PATCH] Upgrade argvParser with option values with ':' --- .gitignore | 0 php/argvParser.php | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/php/argvParser.php b/php/argvParser.php index 05719ac..dcb3f4d 100644 --- a/php/argvParser.php +++ b/php/argvParser.php @@ -25,6 +25,11 @@ class argParsed $this->parsed[$key] = $value; return $this; } + + public function getRest(): string + { + return $this->get('rest', ''); + } } readonly class argvParser @@ -56,18 +61,24 @@ readonly class argvParser $parsed = (new argParsed())->initOptions($this->options); while (str_starts_with($argv[0] ?? '', '-')) { - $option = substr($argv[0], 1); + $option = substr($argv[0], 1, 1); + if (substr($argv[0], 2, 1) === ':') { + $data = substr($argv[0], 3); + } else { + $data = true; + } if ($this->options[$option]) { - $parsed->set($option, true); + $parsed->set($option, $data); } array_shift($argv); } - if (count($argv) !== count($this->arguments)) { + if (count($argv) < count($this->arguments)) { return false; } foreach ($this->arguments as $arg => $description) { $parsed->set($arg, array_shift($argv)); } + $parsed->set('rest', implode(' ', $argv)); return $parsed; }