From 7d79acb5cf9e2c737a11a2108aff06316753b92d Mon Sep 17 00:00:00 2001 From: Eddie Kohler Date: Sat, 20 Jan 2024 18:03:33 -0500 Subject: [PATCH] Getopt: Add dupopt() support. dupopt(false) means non-multiple options can't be given more than once. --- lib/getopt.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/getopt.php b/lib/getopt.php index 11751a8fa4..b45b4b10e0 100644 --- a/lib/getopt.php +++ b/lib/getopt.php @@ -17,6 +17,8 @@ class Getopt { private $allmulti = false; /** @var ?bool */ private $otheropt = false; + /** @var ?bool */ + private $dupopt = true; /** @var bool */ private $interleave = false; /** @var ?int */ @@ -152,6 +154,13 @@ function otheropt($otheropt) { return $this; } + /** @param ?bool $dupopt + * @return $this */ + function dupopt($dupopt) { + $this->dupopt = $dupopt; + return $this; + } + /** @param bool $interleave * @return $this */ function interleave($interleave) { @@ -527,6 +536,9 @@ function parse($argv) { if (!array_key_exists($name, $res)) { $res[$name] = $pot >= self::MARG ? [$value] : $value; } else if ($pot < self::MARG && !$this->allmulti) { + if (!$this->dupopt) { + throw new CommandLineException("`{$oname}` was given multiple times", $this); + } $res[$name] = $value; } else if (is_array($res[$name])) { $res[$name][] = $value;