From 046b9ca533ea5f9d2834a2b5d3df5a497ba766ee Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Mon, 6 Sep 2021 18:15:11 +0200 Subject: [PATCH] Correction erreur format retour "getMaxOccur" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + Changement traitement valeur par défaut des arguments --- .../Argument/Value/ValueAbstract.php | 8 ++-- src/CommandLine/CommandLine.php | 47 +++++++++---------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/CommandLine/Argument/Value/ValueAbstract.php b/src/CommandLine/Argument/Value/ValueAbstract.php index 1c43eec..756808c 100644 --- a/src/CommandLine/Argument/Value/ValueAbstract.php +++ b/src/CommandLine/Argument/Value/ValueAbstract.php @@ -20,7 +20,7 @@ abstract class ValueAbstract extends ArgumentAbstract implements IArgumentValue */ protected int $occurMin = 1; /** - * @var int|null Le nombre maximum d'occurence. + * @var int|null Le nombre maximum d'occurence. Null si illimité */ protected ?int $occurMax = 1; @@ -94,13 +94,13 @@ abstract class ValueAbstract extends ArgumentAbstract implements IArgumentValue /** * @inheritDoc */ - public function getOccurMax (): int { + public function getOccurMax (): ?int { return $this->occurMax; } /** - * Définit le nombre mawimum d'occurence. + * Définit le nombre maximum d'occurence. * - * @param int|null $occurMax Le nombre maximum + * @param int|null $occurMax Le nombre maximum. Null si illimité * * @return $this * @see $occurMax diff --git a/src/CommandLine/CommandLine.php b/src/CommandLine/CommandLine.php index 03eba70..a14cfda 100644 --- a/src/CommandLine/CommandLine.php +++ b/src/CommandLine/CommandLine.php @@ -1051,8 +1051,16 @@ class CommandLine { public function parseExplicit (array $argv): stdClass { $stop = false; $nb_args = count($argv); + $out = new stdClass(); - $out = $this->_parseOptions($argv, $this->getOptions(), $stop); + // Valeurs par défaut + foreach ($this->getOptions() as $option) { + if (!isset($out->{$option->getVarName()})) { + $out->{$option->getVarName()} = $option->getDefault(); + } + } + + $this->_parseOptions($argv, $out, $this->getOptions(), $stop); if ($stop) { return $out; } @@ -1062,6 +1070,14 @@ class CommandLine { } $values = array_values($this->getValues()); + + // Valeurs par défaut + foreach ($values as $value) { + if (!isset($out->{$value->getVarName()})) { + $out->{$value->getVarName()} = $value->getDefault(); + } + } + /** * @var int $ordre * @var IArgumentValue $value @@ -1088,18 +1104,6 @@ class CommandLine { throw new TooMuchValues('Trop de paramètres : ' . count($argv) . ' / ' . $nb_args); } - // Valeurs par défaut - foreach ($this->getOptions() as $option) { - if (!isset($out->{$option->getVarName()})) { - $out->{$option->getVarName()} = $option->getDefault(); - } - } - foreach ($values as $value) { - if (!isset($out->{$value->getVarName()})) { - $out->{$value->getVarName()} = $value->getDefault(); - } - } - return $out; } /** @@ -1164,16 +1168,14 @@ class CommandLine { * Parse des arguments "option" * * @param string[] $argv La liste des arguments du script + * @param stdClass $out Les valeurs de sortie * @param IArgumentOption[] $options La liste des options * @param boolean $stop Arrêt du parsage ? * - * @return stdClass Les options extraites * @throws IncorrectParse Si le parsage d'une des options échoue */ - protected static function _parseOptions (array &$argv, array $options, bool &$stop): stdClass { - $values = new stdClass(); + protected static function _parseOptions (array &$argv, stdClass &$out, array $options, bool &$stop): void { $stop = false; - foreach ($options as $option) { do { $argv_option = $argv; @@ -1184,14 +1186,13 @@ class CommandLine { if (!is_null($result)) { if ($option->isStoppingParse()) { - $values = new stdClass(); + $out = new stdClass(); } - self::_setValue($values, $option, $result, $option->allowMultiple() ? 2 : 1); + self::_setValue($out, $option, $result, $option->allowMultiple() ? 2 : 1); if ($option->isStoppingParse()) { $stop = true; - return $values; } array_splice($argv, count($argv) - count($argv_option), $result->getConsume()); @@ -1203,8 +1204,6 @@ class CommandLine { } } while (count($argv_option) > 1 && (!$find || $option->allowMultiple())); } - - return $values; } /** * Parse un argument "value" X fois @@ -1218,7 +1217,7 @@ class CommandLine { * @throws MissingArgument Si l'argument n'est pas en quantité suffisante * @throws IncorrectParse Si l'argument échoue à se parser */ - protected static function _parseXTimes (array &$argv, stdClass $out, IArgumentValue $value, int $xTimes, int $offset = 0) { + protected static function _parseXTimes (array &$argv, stdClass &$out, IArgumentValue $value, int $xTimes, int $offset = 0) { if ($xTimes > count($argv)) { throw new MissingArgument('L\'argument ' . $value->getName() . ' est en quantité insuffisante (' . count($argv) . ' / ' . $xTimes . ')'); } @@ -1241,7 +1240,7 @@ class CommandLine { * @param ParseResult $result Le résultat du parsage de l'argument * @param int|null $max Le nombre maximum de valeur autorisé */ - protected static function _setValue (stdClass $out, IArgument $argument, ParseResult $result, ?int $max) { + protected static function _setValue (stdClass &$out, IArgument $argument, ParseResult $result, ?int $max) { if (is_null($max) || $max > 1) { if (!isset($out->{$argument->getVarName()})) { $out->{$argument->getVarName()} = array();