@ -19,6 +19,7 @@ use jrosset\CommandLine\Exception\TooMuchValues;
use ReflectionClass;
use ReflectionException;
use stdClass;
use Throwable;
/**
* Une ligne de commande (description, arguments, etc.)
@ -60,33 +61,33 @@ class CommandLine {
/**
* @var string|null La commande de lancement du script (si non fourni, égal au {@see CommandeLine::_programName nom du programme})
*/
protected $_ command;
protected ?string $command;
/**
* @var string Le nom du programme
*/
protected $_ programName;
protected string $programName;
/**
* @var string Description du programme
*/
protected $_ description;
protected string $description;
/**
* @var string|null La version du programme.
*/
protected $_ version;
protected ?string $version;
/**
* @var IArgumentOption[] Liste des options de la ligne de commande
*/
protected $_arguments_o ptions = array();
protected array $argumentsO ptions = array();
/**
* @var IArgumentValue[] Liste _ordonnée_ des arguments "valeur" de la ligne de commande
*/
protected $_arguments_v alues = array();
protected array $argumentsV alues = array();
/**
* @var string[] Liste des codes de retour avec leur descriptif
*/
protected $_ exitCodes = array();
protected array $exitCodes = array();
/**
* Crée un ligne de commande
@ -98,7 +99,7 @@ class CommandLine {
*
* @throws InvalidArgumentException
*/
public function __construct ($programName, $mainDescription, $command = null, $version = null) {
public function __construct (string $programName, string $mainDescription, ?string $command = null, ?string $version = null) {
$this->setProgramName($programName);
$this->setDescription($mainDescription);
$this->setCommand($command);
@ -108,13 +109,13 @@ class CommandLine {
/**
* La commande de lancement du programme
*
* NULL = {@see CommandLine::$_ programName Nom du programme}
* NULL = {@see CommandLine::$programName Nom du programme}
*
* @return string|null La commande
* @see CommandLine::$_ command
* @see CommandLine::$command
*/
public function getCommand () {
return $this->_ command;
public function getCommand (): ?string {
return $this->command;
}
/**
* Modifie la commande de lancement du programme
@ -122,10 +123,10 @@ class CommandLine {
* @param string|null $command La nouvelle commande
*
* @return $this
* @see CommandLine::$_ command
* @see CommandLine::$command
*/
public function setCommand ($command) {
$this->_ command = $command;
public function setCommand (?string $command): self {
$this->command = $command;
return $this;
}
@ -133,10 +134,10 @@ class CommandLine {
* Le nom du programme.
*
* @return string Le nom.
* @see CommandLine::$_ programName
* @see CommandLine::$programName
*/
public function getProgramName () {
return $this->_ programName;
public function getProgramName (): string {
return $this->programName;
}
/**
* Modifie le nom du programme.
@ -144,10 +145,10 @@ class CommandLine {
* @param string $programName Le nouveau nom
*
* @return $this
* @see CommandLine::$_ programName
* @see CommandLine::$programName
*/
public function setProgramName ($programName) {
$this->_ programName = $programName;
public function setProgramName (string $programName): self {
$this->programName = $programName;
return $this;
}
@ -155,10 +156,10 @@ class CommandLine {
* La description du programme
*
* @return string La description
* @see CommandLine::$_ description
* @see CommandLine::$description
*/
public function getDescription () {
return $this->_ description;
public function getDescription (): string {
return $this->description;
}
/**
* Modifie la description du programme.
@ -166,10 +167,10 @@ class CommandLine {
* @param string $description La nouvelle description.
*
* @return $this
* @see CommandLine::$_ description
* @see CommandLine::$description
*/
public function setDescription ($description) {
$this->_ description = $description;
public function setDescription (string $description): self {
$this->description = $description;
return $this;
}
@ -177,10 +178,10 @@ class CommandLine {
* La version du programme
*
* @return string|null La version
* @see CommandLine::$_ version
* @see CommandLine::$version
*/
public function getVersion () {
return $this->_ version;
public function getVersion (): ?string {
return $this->version;
}
/**
* Modifie la version du programme.
@ -188,10 +189,10 @@ class CommandLine {
* @param string|null $version La nouvelle version.
*
* @return $this
* @see CommandLine::$_ version
* @see CommandLine::$version
*/
public function setVersion ($version) {
$this->_ version = $version;
public function setVersion (?string $version): self {
$this->version = $version;
return $this;
}
@ -199,10 +200,10 @@ class CommandLine {
* La liste des arguments "value"
*
* @return IArgumentValue[] La liste des valeurs
* @see CommandLine::$_arguments_v alues
* @see CommandLine::$argumentsV alues
*/
public function getValues () {
return $this->_arguments_v alues;
public function getValues (): array {
return $this->argumentsV alues;
}
/**
* Modifie la liste des arguments "value"
@ -213,12 +214,12 @@ class CommandLine {
* @param IArgumentValue[] $values La nouvelle liste de valeurs
*
* @return $this
* @see CommandLine::$_arguments_v alues
* @see CommandLine::$argumentsV alues
*/
public function setValues ($values) {
public function setValues (array $values): self {
self::_checkValueList($values);
$this->_arguments_v alues = $values;
$this->argumentsV alues = $values;
return $this;
}
/**
@ -229,15 +230,15 @@ class CommandLine {
* @param IArgumentValue[]|IArgumentValue $values La liste des valeurs à ajouter
*
* @return $this
* @see CommandLine::$_arguments_v alues
* @see CommandLine::$argumentsV alues
*/
public function addValues ($values) {
public function addValues ($values): self {
if (!is_array($values) & & $values instanceof IArgumentValue) {
$values = array($values);
}
self::_checkValueList($values);
$this->_arguments_values = array_merge($this->_arguments_v alues, $values);
$this->argumentsValues = array_merge($this->argumentsV alues, $values);
return $this;
}
/**
@ -248,9 +249,9 @@ class CommandLine {
* @param IArgumentValue $value La valeur à ajouter
*
* @return $this
* @see CommandLine::$_arguments_v alues
* @see CommandLine::$argumentsV alues
*/
public function addValue ($value) {
public function addValue (IArgumentValue $value): self {
return $this->addValues(array($value));
}
/**
@ -260,8 +261,8 @@ class CommandLine {
*
* @return bool True si la valeur existe déjà, sinon False
*/
public function existsValue ($valueName) {
foreach ($this->_arguments_v alues as $option) {
public function existsValue (string $valueName): bool {
foreach ($this->argumentsV alues as $option) {
if ($option->getName() == $valueName) {
return true;
}
@ -276,7 +277,7 @@ class CommandLine {
*
* @return bool True si c'est bien un argument "value", sinon False
*/
public static function isValue ($argument) {
public static function isValue ($argument): bool {
return $argument instanceof IArgumentValue;
}
/**
@ -286,7 +287,7 @@ class CommandLine {
*
* @param IArgumentValue[] $values Les valeurs à vérifier
*/
protected static function _checkValueList ($values) {
protected static function _checkValueList (array $values) {
if (!is_array($values)) {
throw new InvalidArgumentException('La liste des valeurs n\'est pas un tableau');
}
@ -301,10 +302,10 @@ class CommandLine {
* La liste des arguments "option"
*
* @return IArgumentOption[] La liste des options
* @see CommandLine::$_arguments_o ptions
* @see CommandLine::$argumentsO ptions
*/
public function getOptions () {
return $this->_arguments_o ptions;
public function getOptions (): array {
return $this->argumentsO ptions;
}
/**
* Modifie la liste des arguments "option"
@ -315,12 +316,12 @@ class CommandLine {
* @param IArgumentOption[] $options La nouvelle liste d'options
*
* @return $this
* @see CommandLine::$_arguments_o ptions
* @see CommandLine::$argumentsO ptions
*/
public function setOptions ($options) {
public function setOptions (array $options): self {
self::_checkOptionList($options);
$this->_arguments_o ptions = $options;
$this->argumentsO ptions = $options;
return $this;
}
/**
@ -331,15 +332,15 @@ class CommandLine {
* @param IArgumentOption[]|IArgumentOption $options La liste d'options à ajouter
*
* @return $this
* @see CommandLine::$_arguments_o ptions
* @see CommandLine::$argumentsO ptions
*/
public function addOptions ($options) {
public function addOptions ($options): self {
if (!is_array($options) & & $options instanceof IArgumentOption) {
$options = array($options);
}
self::_checkOptionList($options);
$this->_arguments_options = array_merge($this->_arguments_o ptions, $options);
$this->argumentsOptions = array_merge($this->argumentsO ptions, $options);
return $this;
}
/**
@ -350,9 +351,9 @@ class CommandLine {
* @param IArgumentOption $option L'options à ajouter
*
* @return $this
* @see CommandLine::$_arguments_o ptions
* @see CommandLine::$argumentsO ptions
*/
public function addOption ($option) {
public function addOption (IArgumentOption $option): self {
return $this->addOptions(array($option));
}
/**
@ -362,8 +363,8 @@ class CommandLine {
*
* @return bool True si l'option existe déjà, sinon False
*/
public function existsOption ($optionName) {
foreach ($this->_arguments_o ptions as $option) {
public function existsOption (string $optionName): bool {
foreach ($this->argumentsO ptions as $option) {
if ($option->getName() == $optionName) {
return true;
}
@ -378,7 +379,7 @@ class CommandLine {
*
* @return bool True si c'est bien un argument "option", sinon False
*/
public static function isOption ($argument) {
public static function isOption ($argument): bool {
return $argument instanceof IArgumentOption;
}
/**
@ -388,7 +389,7 @@ class CommandLine {
*
* @param IArgumentOption[] $options Les options à vérifier
*/
protected static function _checkOptionList ($options) {
protected static function _checkOptionList (array $options) {
if (!is_array($options)) {
throw new InvalidArgumentException('La liste des options n\'est pas un tableau');
}
@ -404,7 +405,7 @@ class CommandLine {
*
* @return IArgument[] La liste des arguments
*/
public function getArguments () {
public function getArguments (): array {
return array_merge($this->getValues(), $this->getOptions());
}
/**
@ -417,7 +418,7 @@ class CommandLine {
*
* @return $this
*/
public function setArguments ($arguments) {
public function setArguments (array $arguments): self {
$this->setOptions(array());
$this->setValues(array());
@ -432,7 +433,7 @@ class CommandLine {
*
* @return $this
*/
public function addArguments ($arguments) {
public function addArguments ($arguments): self {
if (!is_array($arguments) & & $arguments instanceof IArgumentValue) {
$arguments = array($arguments);
}
@ -453,7 +454,7 @@ class CommandLine {
*
* @return $this
*/
public function addArgument ($argument) {
public function addArgument (IArgument $argument): self {
if (!self::isArgument($argument)) {
throw new InvalidArgumentException('L\'argument n\'est pas valide (n\'implémente pas IArgument)');
}
@ -477,6 +478,7 @@ class CommandLine {
}
}
}
/** @noinspection PhpRedundantCatchClauseInspection */
catch (ReflectionException $e) {
$type = /** @lang text */
'< inconnu > ';
@ -493,8 +495,10 @@ class CommandLine {
* @param string $argumentName Le nom de l'argument
*
* @return bool True si l'argument existe déjà, sinon False
*
* @noinspection PhpUnused
*/
public function existsArgument ($argumentName) {
public function existsArgument (string $argumentName): bool {
$arguments = $this->getArguments();
foreach ($arguments as $argument) {
if ($argument->getName() == $argumentName) {
@ -511,7 +515,7 @@ class CommandLine {
*
* @return bool True si c'est bien un argument, sinon False
*/
public static function isArgument ($argument) {
public static function isArgument ($argument): bool {
return $argument instanceof IArgument;
}
/**
@ -520,8 +524,10 @@ class CommandLine {
* Doit être un tableau et chaque élément doit implémenter {@see IArgument}
*
* @param IArgument[] $arguments Les arguments à vérifier
*
* @noinspection PhpUnused
*/
protected static function _checkArgumentList ($arguments) {
protected static function _checkArgumentList (array $arguments) {
if (!is_array($arguments)) {
throw new InvalidArgumentException('La liste des arguments n\'est pas un tableau');
}
@ -537,8 +543,8 @@ class CommandLine {
*
* @return string[] La liste des codes de retour avec leur descriptifs
*/
public function getExitCodes () {
return $this->_ exitCodes;
public function getExitCodes (): array {
return $this->exitCodes;
}
/**
* La description de l'un de code de retour
@ -549,11 +555,11 @@ class CommandLine {
*
* @throws InvalidArgumentException Si le code de retour n'est pas un entier
*/
public function getExitCodeDescription ($code) {
public function getExitCodeDescription (int $code): ?string {
if (filter_var($code, FILTER_VALIDATE_INT) === false) {
throw new InvalidArgumentException('Le code de retour "' . $code . '" n\'est pas un entier');
}
return $this->_ exitCodes[(int) $code];
return $this->exitCodes[$code];
}
/**
* Remplace la liste des codes de retour avec leur descriptif
@ -565,8 +571,8 @@ class CommandLine {
* @throws InvalidArgumentException Si la clé du tableaux (les codes) ne sont pas des entiers ou
* que la valeur (description) n'est pas convertible en chaine de caractères
*/
public function setExitCodes ($exitCodes = array()) {
$this->_ exitCodes = array();
public function setExitCodes (array $exitCodes = array()): self {
$this->exitCodes = array();
return $this->addExitCodes($exitCodes);
}
/**
@ -579,7 +585,7 @@ class CommandLine {
* @throws InvalidArgumentException Si la clé du tableaux (les codes) ne sont pas des entiers ou
* que la valeur (description) n'est pas convertible en chaine de caractères
*/
public function addExitCodes ($exitCodes) {
public function addExitCodes (array $exitCodes): self {
foreach ($exitCodes as $code => $description) {
$this->addExitCode($code, $description);
}
@ -597,7 +603,7 @@ class CommandLine {
* @throws InvalidArgumentException Si le code n'est pas un entier ou
* que la description n'est pas convertible en chaine de caractères
*/
public function addExitCode ($code, $description = null) {
public function addExitCode (int $code, ?string $description = null): self {
if (filter_var($code, FILTER_VALIDATE_INT) === false) {
throw new InvalidArgumentException('Le code de retour "' . $code . '" n\'est pas un entier');
}
@ -610,7 +616,7 @@ class CommandLine {
throw new InvalidArgumentException('La description "' . $code . '" n\'est pas convertible en chaine de caractères');
}
$this->_ exitCodes[(int) $code] = (string)$description;
$this->exitCodes[$code] = (string)$description;
return $this;
}
/**
@ -622,11 +628,11 @@ class CommandLine {
*
* @throws InvalidArgumentException Si le code de retour n'est pas un entier
*/
public function existsExitCode ($code) {
public function existsExitCode (int $code): bool {
if (filter_var($code, FILTER_VALIDATE_INT) === false) {
throw new InvalidArgumentException('Le code de retour "' . $code . '" n\'est pas un entier');
}
return array_key_exists((int) $code, $this->_ exitCodes);
return array_key_exists($code, $this->exitCodes);
}
/**
@ -640,7 +646,7 @@ class CommandLine {
*
* @return $this
*/
public function addDefaultArguments ($shortForVersion = true) {
public function addDefaultArguments (bool $shortForVersion = true): self {
if (!$this->existsOption(self::ARGUMENT_OPTION_HELP)) {
$this->addOption(
(new Argument\Option\Flag(self::ARGUMENT_OPTION_HELP, false, 'Affiche cette aide', 'help', 'h'))
@ -663,7 +669,7 @@ class CommandLine {
* @param stdClass $values Les valeurs du parsage
* @param boolean $exitAtEnd Terminer le script à la fin de la fonction correspondante ?
*/
public function treatDefaultArguments ($values, $exitAtEnd = true) {
public function treatDefaultArguments (stdClass $values, bool $exitAtEnd = true) {
if (isset($values->{self::ARGUMENT_OPTION_HELP}) & & $values->{self::ARGUMENT_OPTION_HELP} === true) {
$this->showHelp($exitAtEnd);
}
@ -680,7 +686,7 @@ class CommandLine {
*
* @see CommandLine::getVersion()
*/
public function showVersion ($exitAtEnd = true) {
public function showVersion (bool $exitAtEnd = true) {
echo $this->getVersion() . "\n";
if ($exitAtEnd) {
@ -694,7 +700,7 @@ class CommandLine {
*
* @see CommandLine::generateHelp()
*/
public function showHelp ($exitAtEnd = true) {
public function showHelp (bool $exitAtEnd = true) {
echo $this->generateHelp() . "\n";
if ($exitAtEnd) {
@ -708,7 +714,7 @@ class CommandLine {
*
* @see CommandLine::showHelp()
*/
public function generateHelp () {
public function generateHelp (): string {
$help = array();
$help[] = $this->getProgramName() . (is_null($this->getVersion()) ? '' : ', version ' . $this->getVersion());
@ -719,21 +725,21 @@ class CommandLine {
empty($this->getCommand()) ? $this->getProgramName() : $this->getCommand(),
count($this->getOptions()) > 0 ? '[OPTIONS]' : '',
);
$syntax = array_merge($syntax, array_map(array(__CLASS__, '_ getSyntaxOfValue'), $this->getValues()));
$syntax = array_merge($syntax, array_map(array(__CLASS__, 'getSyntaxOfValue'), $this->getValues()));
$help[] = implode(' ', $syntax);
$help[] = 'Arguments :';
$help = array_merge($help, self::_ getValuesListing($this->getValues()));
$help = array_merge($help, self::getValuesListing($this->getValues()));
$help[] = '';
$help[] = 'Options :';
$help = array_merge($help, self::_ getOptionsListing($this->getOptions()));
$help = array_merge($help, self::getOptionsListing($this->getOptions()));
$help[] = '';
if (count($this->getExitCodes()) > 0) {
$help[] = 'Exit codes :';
$help = array_merge($help, self::_ getExitCodesListing($this->getExitCodes()));
$help = array_merge($help, self::getExitCodesListing($this->getExitCodes()));
$help[] = '';
}
@ -748,7 +754,7 @@ class CommandLine {
* @return string La syntax de l'argument
* @see generateHelp()
*/
protected static function _ getSyntaxOfValue (IArgumentValue $value) {
protected static function getSyntaxOfValue (IArgumentValue $value): string {
$syntax = '';
$min = $value->getOccurMin();
@ -781,13 +787,13 @@ class CommandLine {
return $syntax;
}
/**
* Ge nère l'aide d'une liste d'arguments "value"
* Gé nère l'aide d'une liste d'arguments "value"
*
* @param IArgumentValue[] $values La liste des valeurs
*
* @return string[] L'aide de chaque valeur
*/
protected static function _ getValuesListing ($values) {
protected static function getValuesListing (array $values): array {
/*
* Calcul des différents padding
*/
@ -814,13 +820,13 @@ class CommandLine {
}
$spaces = array();
$spaces[] = str_pad('', $pads->name, ' ' );
$spaces[] = str_pad('', $pads->occurMin + 4 + $pads->occurMax, ' ' );
$spaces[] = str_pad('', $pads->name);
$spaces[] = str_pad('', $pads->occurMin + 4 + $pads->occurMax);
if ($pads->valueDescription > 0) {
$spaces[] = str_pad('', $pads->valueDescription, ' ' );
$spaces[] = str_pad('', $pads->valueDescription);
}
if ($pads->default > 0) {
$spaces[] = str_pad('', $pads->default, ' ' );
$spaces[] = str_pad('', $pads->default);
}
$spaces[] = '';
@ -834,8 +840,7 @@ class CommandLine {
$max = $value->getOccurMax();
$occur = '';
$occur .= str_pad($value->getOccurMin(), $pads->occurMin, ' ', STR_PAD_LEFT);
$occur = str_pad($value->getOccurMin(), $pads->occurMin, ' ', STR_PAD_LEFT);
$occur .= ' => ';
$occur .= str_pad(is_null($max) ? 'N' : $max, $pads->occurMax, ' ', STR_PAD_RIGHT);
$entry[] = $occur;
@ -864,13 +869,13 @@ class CommandLine {
return $entries;
}
/**
* Ge nère l'aide d'une liste d'arguments "option"
* Gé nère l'aide d'une liste d'arguments "option"
*
* @param IArgumentOption[] $options La liste des options
*
* @return string[] L'aide de chaque option
*/
protected static function _ getOptionsListing ($options) {
protected static function getOptionsListing (array $options): array {
/*
* Calcul des différents padding
*/
@ -900,12 +905,12 @@ class CommandLine {
$pads->tagLong += 2;
$spaces = array();
$spaces[] = str_pad('', $pads->tagShort + 1 + $pads->tagLong + 2, ' ' );
$spaces[] = str_pad('', $pads->tagShort + 1 + $pads->tagLong + 2);
if ($pads->valueDescription > 0) {
$spaces[] = str_pad('', $pads->valueDescription, ' ' );
$spaces[] = str_pad('', $pads->valueDescription);
}
if ($pads->default > 0) {
$spaces[] = str_pad('', $pads->default, ' ' );
$spaces[] = str_pad('', $pads->default);
}
$spaces[] = '';
@ -918,8 +923,7 @@ class CommandLine {
$short = $option->getTagShort();
$label = '';
$label .= str_pad(is_null($short) ? '' : '-' . $short, $pads->tagShort, ' ', STR_PAD_RIGHT);
$label = str_pad(is_null($short) ? '' : '-' . $short, $pads->tagShort, ' ', STR_PAD_RIGHT);
$label .= ' ';
$label .= str_pad('--' . $option->getTagLong(), $pads->tagLong, ' ', STR_PAD_RIGHT);
$label .= $option->allowMultiple() ? ' *' : ($option->isStoppingParse() ? ' X' : ' ');
@ -956,7 +960,7 @@ class CommandLine {
*
* @return string[] L'aide de chaque code de retour
*/
protected static function _ getExitCodesListing ($exitCodes) {
protected static function getExitCodesListing (array $exitCodes): array {
/*
* Calcul des différents padding
*/
@ -969,8 +973,6 @@ class CommandLine {
$pads->codes = max($pads->codes, strlen($code));
}
$spaces[] = str_pad('', $pads->tagShort + 1 + $pads->tagLong + 2, ' ');
/*
* Génération des descriptifs
*/
@ -997,17 +999,45 @@ class CommandLine {
* Revient à appeler {@see CommandLine::parseExplicit()} avec les arguments du script {@link https://www.php.net/manual/en/reserved.variables.argv.php $_SERVER['argv']}
*
* @return stdClass Les valeurs extraites
*
* @throws MissingArgument Quand un argument de la liste est manquant ou en quantité insuffisante
* @throws TooMuchValues Quand il reste des valeurs supplémentaires après le traitement de la liste d'arguments
*/
public function parse () {
public function parse (): stdClass {
$argv = $_SERVER['argv'];
array_shift($argv); // Supprime le 1er paramètre : le nom du script PHP
return $this->parseExplicit($argv);
}
/**
* Traite une liste d'arguments.
* Traite les arguments du script
*
* Le lève pas d'exceptions : le message d'erreur est redirigé vers $file
*
* @param int|null $exitCode Null si n'arrête pas l'exécution, sinon le code retour (exit)
* @param resource $file Le fichier dans lequel écrire l'erreur
*
* @return stdClass|null Les valeurs extraites (Null si erreur)
*
* @see CommandLine::parse()
*
* @noinspection PhpMissingParamTypeInspection
*/
public function parseNoExcept (?int $exitCode = null, $file = STDERR): ?stdClass {
try {
return $this->parse();
}
catch (Throwable $e) {
fwrite($file, $e->getMessage());
if ($exitCode !== null) {
exit($exitCode);
}
return null;
}
}
/**
* Traite une liste d'arguments
*
* 1/ Vérifie que la liste d'arguments correspond à la syntaxe de la ligne de commande
* 2/ Extrait les valeurs voulues de la liste d'arguments
@ -1018,7 +1048,7 @@ class CommandLine {
* @throws MissingArgument Quand un argument de la liste est manquant ou en quantité insuffisante
* @throws TooMuchValues Quand il reste des valeurs supplémentaires après le traitement de la liste d'arguments
*/
public function parseExplicit ($argv) {
public function parseExplicit (array $argv): stdClass {
$stop = false;
$nb_args = count($argv);
@ -1072,6 +1102,36 @@ class CommandLine {
return $out;
}
/**
* Traite une liste d'arguments
*
* Le lève pas d'exceptions : le message d'erreur est redirigé vers $file
*
* @param string[] $argv La liste d'arguments
* @param int|null $exitCode Null si n'arrête pas l'exécution, sinon le code retour (exit)
* @param resource $file Le fichier dans lequel écrire l'erreur
*
* @return stdClass|null Les valeurs extraites (Null si erreur)
*
* @see CommandLine::parseExplicit()
*
* @noinspection PhpUnused
* @noinspection PhpMissingParamTypeInspection
*/
public function parseExplicitNoExcept (array $argv, ?int $exitCode = null, $file = STDERR): ?stdClass {
try {
return $this->parseExplicit($argv);
}
catch (Throwable $e) {
fwrite($file, $e->getMessage());
if ($exitCode !== null) {
exit($exitCode);
}
return null;
}
}
/**
* Calcule le nombre d'argument "value" restant à honorer.
*
@ -1079,7 +1139,7 @@ class CommandLine {
*
* @return int Le nombre de valeurs restantes à traiter, ou -1 si un nombre "illimité"
*/
protected function _getNbArgumentRestant ($ordre_start) {
protected function _getNbArgumentRestant (int $ordre_start): int {
$nb = 0;
/**
* @var int $ordre
@ -1110,7 +1170,7 @@ class CommandLine {
* @return stdClass Les options extraites
* @throws IncorrectParse Si le parsage d'une des options échoue
*/
protected static function _parseOptions (& $argv, $options, & $stop) {
protected static function _parseOptions (array & $argv, array $options, bool & $stop): stdClass {
$values = new stdClass();
$stop = false;
@ -1158,7 +1218,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 (& $argv, & $out, $value, $xTimes, $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 . ')');
}
@ -1181,7 +1241,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 (& $out, $argument, $result, $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();