Skip to content

Commit

Permalink
Getopt: Add dupopt() support.
Browse files Browse the repository at this point in the history
dupopt(false) means non-multiple options can't be given more than once.
  • Loading branch information
kohler committed Jan 20, 2024
1 parent 4446089 commit 7d79acb
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/getopt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7d79acb

Please sign in to comment.