v2
parent
046b9ca533
commit
7351f90e28
@ -0,0 +1,114 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine\Argument;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract base for arguments
|
||||||
|
*/
|
||||||
|
abstract class AbstractArgument implements IArgument {
|
||||||
|
/**
|
||||||
|
* @var string The variable name in parse output
|
||||||
|
*/
|
||||||
|
private string $varName;
|
||||||
|
/**
|
||||||
|
* @var mixed|null The default value for the argument. Null if none
|
||||||
|
*/
|
||||||
|
private $_default;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string The argument name in help
|
||||||
|
*/
|
||||||
|
private string $name;
|
||||||
|
/**
|
||||||
|
* @var string|null The argument description in help
|
||||||
|
*/
|
||||||
|
private ?string $description;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new argument
|
||||||
|
*
|
||||||
|
* By default, the {@see AbstractArgument::$varName variable name} is the same as the {@see AbstractArgument::$name argument in help}
|
||||||
|
*
|
||||||
|
* @param string $name The argument name in help
|
||||||
|
* @param string|null $description The argument description in help
|
||||||
|
*/
|
||||||
|
protected function __construct (string $name, ?string $description) {
|
||||||
|
$this->setVarName($this->getName());
|
||||||
|
$this->setDefault();
|
||||||
|
|
||||||
|
$this->setName($name);
|
||||||
|
$this->setDescription($description);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getVarName (): string {
|
||||||
|
return $this->varName;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the variable name in parse output
|
||||||
|
*
|
||||||
|
* @param string $varName The variable name
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setVarName (string $varName): self {
|
||||||
|
$this->varName = $varName;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getDefault () {
|
||||||
|
return $this->_default;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the default value for the argument. Null if none
|
||||||
|
*
|
||||||
|
* @param mixed $default The default value
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setDefault ($default = null): self {
|
||||||
|
$this->_default = $default;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getName (): string {
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the argument name in help
|
||||||
|
*
|
||||||
|
* @param string $name The argument name in help
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setName (string $name): self {
|
||||||
|
$this->name = $name;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getDescription (): ?string {
|
||||||
|
return $this->description;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the argument description in help
|
||||||
|
*
|
||||||
|
* @param string|null $description The argument description
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setDescription (?string $description): self {
|
||||||
|
$this->description = $description;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine\Argument;
|
||||||
|
|
||||||
|
use jrosset\CommandLine\ArrayClass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of arguments
|
||||||
|
*
|
||||||
|
* @extends ArrayClass<IArgument>
|
||||||
|
*/
|
||||||
|
abstract class AbstractArgumentList extends ArrayClass {
|
||||||
|
/**
|
||||||
|
* Get the key of an element. Null if auto-generated
|
||||||
|
*
|
||||||
|
* @param IArgument $element The element
|
||||||
|
*
|
||||||
|
* @return int The key or Null
|
||||||
|
*/
|
||||||
|
protected function getKeyForElement ($element): int {
|
||||||
|
return $element->getVarName();
|
||||||
|
}
|
||||||
|
}
|
@ -1,126 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Déclaration de la classe CommandLine\Argument\AbstractArgument.
|
|
||||||
*/
|
|
||||||
namespace jrosset\CommandLine\Argument;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Classe abstraite pour n'importe quel type de paramètre
|
|
||||||
*
|
|
||||||
* @package CommandLine\Argument
|
|
||||||
*/
|
|
||||||
abstract class ArgumentAbstract implements IArgument {
|
|
||||||
/**
|
|
||||||
* @var string Le nom de la variable de retour de l'argument.
|
|
||||||
*/
|
|
||||||
protected string $varName;
|
|
||||||
/**
|
|
||||||
* @var string Le nom de l'argument.
|
|
||||||
*/
|
|
||||||
protected string $name;
|
|
||||||
/**
|
|
||||||
* @var string|null La description de l'argument.
|
|
||||||
*/
|
|
||||||
protected ?string $description;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var mixed|null La valeur par défaut. Null si aucune.
|
|
||||||
*/
|
|
||||||
protected $_default = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Crée un nouvel argument.
|
|
||||||
*
|
|
||||||
* @param string $name Le nom de l'argument
|
|
||||||
* @param string|null $description La description de l'argument
|
|
||||||
*/
|
|
||||||
protected function __construct (string $name, ?string $description) {
|
|
||||||
$this->setName($name);
|
|
||||||
$this->setDescription($description);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function getVarName (): string {
|
|
||||||
return $this->varName;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit le nom de la variable de retour de l'argument.
|
|
||||||
*
|
|
||||||
* @param string $varName Le nom
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*
|
|
||||||
* @see $name
|
|
||||||
*/
|
|
||||||
public function setVarName (string $varName): self {
|
|
||||||
$this->varName = $varName;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function getName (): string {
|
|
||||||
return $this->name;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit le nom de l'argument.
|
|
||||||
*
|
|
||||||
* @param string $name Le nom
|
|
||||||
* @param boolean $replaceVarName Remplacer également le nom de la variable de retour ?
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*
|
|
||||||
* @see $name
|
|
||||||
*/
|
|
||||||
public function setName (string $name, bool $replaceVarName = true): self {
|
|
||||||
$this->name = $name;
|
|
||||||
if ($replaceVarName) {
|
|
||||||
$this->setVarName($name);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function getDescription (): ?string {
|
|
||||||
return $this->description;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit la description de l'argument.
|
|
||||||
*
|
|
||||||
* @param string|null $description La description
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*
|
|
||||||
* @see $description
|
|
||||||
*/
|
|
||||||
public function setDescription (?string $description): self {
|
|
||||||
$this->description = $description;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function getDefault () {
|
|
||||||
return $this->_default;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit le valeur par défaut
|
|
||||||
*
|
|
||||||
* @param mixed|null $default La valeur par défaut.
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*
|
|
||||||
* @see $_default
|
|
||||||
*/
|
|
||||||
public function setDefault ($default = null): self {
|
|
||||||
$this->_default = $default;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,50 +1,33 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Déclaration de l'interface CommandLine\Argument\IArgument
|
|
||||||
*/
|
|
||||||
namespace jrosset\CommandLine\Argument;
|
namespace jrosset\CommandLine\Argument;
|
||||||
|
|
||||||
use jrosset\CommandLine\Exception\IncorrectParse;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface que tout argument doit implémenter
|
* Interface of all arguments
|
||||||
*
|
|
||||||
* @package CommandLine\Argument
|
|
||||||
*/
|
*/
|
||||||
interface IArgument {
|
interface IArgument {
|
||||||
/**
|
/**
|
||||||
* Le nom de la variable de sortie l'argument
|
* The variable name in parse output
|
||||||
*
|
*
|
||||||
* @return string Le nom
|
* @return string The variable name
|
||||||
*/
|
*/
|
||||||
public function getVarName (): string;
|
public function getVarName (): string;
|
||||||
/**
|
/**
|
||||||
* Le nom de l'argument
|
* The default value for the argument. Null if none
|
||||||
*
|
|
||||||
* @return string Le nom
|
|
||||||
*/
|
|
||||||
public function getName (): string;
|
|
||||||
/**
|
|
||||||
* La valeur par défaut de l'argument. Null si pas de valeur par défaut
|
|
||||||
*
|
*
|
||||||
* @return mixed|null La valeur par défaut
|
* @return mixed|null The default value
|
||||||
*/
|
*/
|
||||||
public function getDefault();
|
public function getDefault();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La description de l'argument
|
* The argument name in help
|
||||||
*
|
*
|
||||||
* @return string|null La description
|
* @return string Le argument name
|
||||||
*/
|
*/
|
||||||
public function getDescription (): ?string;
|
public function getName (): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse les arguments.
|
* The argument description in help
|
||||||
*
|
|
||||||
* @param $args array La liste des arguments encore à traiter (par ordre de réception)
|
|
||||||
*
|
*
|
||||||
* @return ParseResult|null Le résultat du parsage. Null si rien parsé
|
* @return string|null The argument description
|
||||||
* @throws IncorrectParse Echec du parsage de l'argument
|
|
||||||
*/
|
*/
|
||||||
public function parse (array $args): ?ParseResult;
|
public function getDescription (): ?string;
|
||||||
}
|
}
|
@ -1,19 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Déclaration de l'interface CommandLine\Argument\IArgumentValueDescription
|
|
||||||
*/
|
|
||||||
namespace jrosset\CommandLine\Argument;
|
namespace jrosset\CommandLine\Argument;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface à implementer par les arguments qui souhaitent détailler la valeur attendue
|
* Interface for argument expected value description
|
||||||
*
|
|
||||||
* @package CommandLine\Argument
|
|
||||||
*/
|
*/
|
||||||
interface IArgumentValueDescription {
|
interface IArgumentValueDescription {
|
||||||
/**
|
/**
|
||||||
* La description de la valeur de d'argument.
|
* The expected value description in help
|
||||||
*
|
*
|
||||||
* @return string La description.
|
* @return string The expected value description
|
||||||
*/
|
*/
|
||||||
public function getValueDescription (): string;
|
public function getValueDescription (): string;
|
||||||
}
|
}
|
@ -0,0 +1,101 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine\Argument\Option;
|
||||||
|
|
||||||
|
use jrosset\CommandLine\Argument\AbstractArgument;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract base for option arguments: --XXX or -X
|
||||||
|
*/
|
||||||
|
abstract class AbstractOptionArgument extends AbstractArgument implements IOptionArgument {
|
||||||
|
/**
|
||||||
|
* @var string|null The short tag (the -X form). Null if none
|
||||||
|
*/
|
||||||
|
private ?string $tagShort;
|
||||||
|
/**
|
||||||
|
* @var string The long tag (the --XXX form)
|
||||||
|
*/
|
||||||
|
private string $tagLong;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new option argument
|
||||||
|
*
|
||||||
|
* By default, the {@see AbstractArgument::$varName variable name} is the same as the {@see AbstractArgument::$name argument in help}
|
||||||
|
*
|
||||||
|
* @param string $name The argument name in help
|
||||||
|
* @param string|null $description The argument description in help
|
||||||
|
* @param string|null $tagLong The long tag. If null, deduce from argument name <b>$name</b> (see {@see AbstractOptionArgument::convertNameToTagLong()}
|
||||||
|
* @param string|null $tagShort the short tag
|
||||||
|
*/
|
||||||
|
public function __construct (string $name, ?string $description, ?string $tagLong = null, ?string $tagShort = null) {
|
||||||
|
parent::__construct($name, $description);
|
||||||
|
|
||||||
|
$this->setTagLong($tagLong ?? static::convertNameToTagLong($name));
|
||||||
|
$this->setTagShort($tagShort);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a variable name to a long tag
|
||||||
|
*
|
||||||
|
* Performs the following tasks :
|
||||||
|
* 1. The underscores are replaced by a caret
|
||||||
|
* 2. The upper case letters are replaced by a caret followed with the lower case letter
|
||||||
|
*
|
||||||
|
* @param string $name The name to convert
|
||||||
|
*
|
||||||
|
* @return string The corresponding long tag
|
||||||
|
*/
|
||||||
|
public static function convertNameToTagLong (string $name): string {
|
||||||
|
return preg_replace_callback(
|
||||||
|
'/[A-Z_]/',
|
||||||
|
function ($matches) {
|
||||||
|
return '-' . ($matches[0] === '_' ? '' : '' . mb_strtolower($matches[0]));
|
||||||
|
},
|
||||||
|
$name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getTagShort (): ?string {
|
||||||
|
return $this->tagShort;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the short tag. Null if none
|
||||||
|
*
|
||||||
|
* @param string|null $tagShort The short tag
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setTagShort (string $tagShort = null): self {
|
||||||
|
$this->tagShort = $tagShort;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Is a short tag defined ?
|
||||||
|
*
|
||||||
|
* @return boolean True if a short tag is defined, else False
|
||||||
|
*/
|
||||||
|
public function hasTagShort (): bool {
|
||||||
|
return $this->getTagShort() !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getTagLong (): string {
|
||||||
|
return $this->tagLong;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the long tag
|
||||||
|
*
|
||||||
|
* @param string $tagLong The long tag
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setTagLong (string $tagLong): self {
|
||||||
|
$this->tagLong = $tagLong;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
@ -1,55 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Déclare la classe CommandLine\Argument\Option\OptionFlagWithReverse.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace jrosset\CommandLine\Argument\Option;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Option de type "flag" : vaut True ou False avec son tag inversé.
|
|
||||||
*
|
|
||||||
* Le tag long inversé est automatiquement généré en préfixant avec "no-" sauf si l'initial commence déjà par "no-".
|
|
||||||
* Le tag court inversé est fait de la même manière, mais avec le préfixe "n".
|
|
||||||
*
|
|
||||||
* Exemples :
|
|
||||||
* - "--force" donne "--no-force"
|
|
||||||
* - "-f" donne "-nf"
|
|
||||||
* - "--no-stop" devient "--stop"
|
|
||||||
*
|
|
||||||
* @package CommandLine\Argument\Option
|
|
||||||
*/
|
|
||||||
class FlagWithReverse extends Flag implements IArgumentOptionSecondary {
|
|
||||||
public function getOthersOptions (): array {
|
|
||||||
$tagShort = null;
|
|
||||||
if ($this->hasTagShort()) {
|
|
||||||
if (substr($this->getTagShort(), 0, 1) == 'n') {
|
|
||||||
$tagShort = substr($this->getTagShort(), 1);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$tagShort = 'n' . $this->getTagShort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (substr($this->getTagLong(), 0, 3) == 'no-') {
|
|
||||||
$tagLong = substr($this->getTagLong(), 3);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$tagLong = 'no-' . $this->getTagLong();
|
|
||||||
}
|
|
||||||
|
|
||||||
$name = $this->getName();
|
|
||||||
if (substr($name, 0, 3) == 'no') {
|
|
||||||
$name = strtolower(substr($name, 2, 1)) . substr($name, 3);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$name = 'no' . strtoupper(substr($name, 0, 1)) . substr($name, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
$description = '[INVERSE] ' . $this->getDescription();
|
|
||||||
|
|
||||||
$reverse = new Flag($name, !$this->getDefault(), $description, $tagLong, $tagShort);
|
|
||||||
$reverse->setVarName($this->getVarName());
|
|
||||||
|
|
||||||
return array($reverse);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Déclaration de l'interface CommandLine\Argument\Option\IArgumentOption
|
|
||||||
*/
|
|
||||||
namespace jrosset\CommandLine\Argument\Option;
|
|
||||||
|
|
||||||
use jrosset\CommandLine\Argument\IArgument;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface à implémenter si l'argument est de type "option"
|
|
||||||
*
|
|
||||||
* @package CommandLine\Argument\Option
|
|
||||||
*/
|
|
||||||
interface IArgumentOption extends IArgument {
|
|
||||||
/**
|
|
||||||
* La tag court.
|
|
||||||
*
|
|
||||||
* @return string|null Le tag court.
|
|
||||||
*
|
|
||||||
* @see $_tagShort
|
|
||||||
*/
|
|
||||||
public function getTagShort (): ?string;
|
|
||||||
/**
|
|
||||||
* Le tag long.
|
|
||||||
*
|
|
||||||
* @return string Le tag long.
|
|
||||||
*
|
|
||||||
* @see $_tagLong
|
|
||||||
*/
|
|
||||||
public function getTagLong (): string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Est-ce que l'argument est autorisé plusieurs fois ?
|
|
||||||
*
|
|
||||||
* @return boolean True si l'argument est autorisé plusieurs fois, sinon False
|
|
||||||
*/
|
|
||||||
public function allowMultiple (): bool;
|
|
||||||
/**
|
|
||||||
* Est-ce que l'argument met fin au parsage ?
|
|
||||||
*
|
|
||||||
* Exemple : --help = affiche l'aide et stoppe le programme
|
|
||||||
*
|
|
||||||
* @return boolean True si la présence de l'argument mat fin au parsage, sinon False.
|
|
||||||
*/
|
|
||||||
public function isStoppingParse (): bool;
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Déclaration de l'interface CommandLine\Argument\Option\IArgumentSecondary.
|
|
||||||
*/
|
|
||||||
namespace jrosset\CommandLine\Argument\Option;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface à implémenter si l'argument "option" auto-déclare d'autres arguments
|
|
||||||
*
|
|
||||||
* @package CommandLine\Argument\Option
|
|
||||||
*/
|
|
||||||
interface IArgumentOptionSecondary {
|
|
||||||
/**
|
|
||||||
* La liste des arguments auto-déclarés.
|
|
||||||
*
|
|
||||||
* @return IArgumentOption[] La liste des arguments
|
|
||||||
*/
|
|
||||||
public function getOthersOptions (): array;
|
|
||||||
}
|
|
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine\Argument\Option;
|
||||||
|
|
||||||
|
use jrosset\CommandLine\Argument\IArgument;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface of option arguments: --XXX or -X
|
||||||
|
*/
|
||||||
|
interface IOptionArgument extends IArgument {
|
||||||
|
/**
|
||||||
|
* The short tag (the -X form). Null if none
|
||||||
|
*
|
||||||
|
* @return string|null The short tag
|
||||||
|
*/
|
||||||
|
public function getTagShort (): ?string;
|
||||||
|
/**
|
||||||
|
* The long tag (the --XXX form)
|
||||||
|
*
|
||||||
|
* @return string The long tag
|
||||||
|
*/
|
||||||
|
public function getTagLong (): string;
|
||||||
|
}
|
@ -1,188 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Déclaration de la classe CommandLine\Argument\Option\OptionAbstract.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace jrosset\CommandLine\Argument\Option;
|
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use jrosset\CommandLine\Argument\ArgumentAbstract;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Classe abstraite pour les arguments de type "option" : --xxx / -x
|
|
||||||
*
|
|
||||||
* @package CommandLine\Argument\Option
|
|
||||||
*/
|
|
||||||
abstract class OptionAbstract extends ArgumentAbstract implements IArgumentOption {
|
|
||||||
/**
|
|
||||||
* @var string|null L'argument court (cas d'un seul "-"). Null si aucun.
|
|
||||||
*/
|
|
||||||
protected ?string $tagShort;
|
|
||||||
/**
|
|
||||||
* @var string L'argument long (cas d'un "--").
|
|
||||||
*/
|
|
||||||
protected string $tagLong;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var boolean Est-ce que l'argument est autorisé plusieurs fois ?
|
|
||||||
*/
|
|
||||||
protected bool $multiple = false;
|
|
||||||
/**
|
|
||||||
* @var boolean Est-ce que l'argument met fin au parsage ?
|
|
||||||
*/
|
|
||||||
protected bool $isStoppingParse = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Crée un nouvel argument de type option.
|
|
||||||
*
|
|
||||||
* @param string $name Le nom.
|
|
||||||
* @param string|null $description La description.
|
|
||||||
* @param string|null $tagLong Le tag long.
|
|
||||||
* @param string|null $tagShort Le tag court.
|
|
||||||
*/
|
|
||||||
public function __construct (string $name, ?string $description, ?string $tagLong = null, ?string $tagShort = null) {
|
|
||||||
parent::__construct($name, $description);
|
|
||||||
|
|
||||||
$this->setTagLong($tagLong);
|
|
||||||
$this->setTagShort($tagShort);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Est-ce la valeur correspond au tag court ou long ?
|
|
||||||
*
|
|
||||||
* Utilisé par les classes enfants pour savoir si le tag correspond.
|
|
||||||
*
|
|
||||||
* @param string $arg La valeur à examiner.
|
|
||||||
*
|
|
||||||
* @return boolean True si le tag correspond, sinon False.
|
|
||||||
*/
|
|
||||||
protected function _parseTag (string $arg): bool {
|
|
||||||
if ($this->hasTagShort()) {
|
|
||||||
if ($arg == '-' . $this->getTagShort()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $arg == '--' . $this->getTagLong();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Est-ce que la liste de valeur contient un argument de type "option" ?
|
|
||||||
*
|
|
||||||
* @param string[] $args La liste de valeurs à tester
|
|
||||||
*
|
|
||||||
* @return string|false La valeur qui une option au False si aucune correspondance
|
|
||||||
*/
|
|
||||||
public static function containsOption (array $args) {
|
|
||||||
foreach ($args as $arg) {
|
|
||||||
if (preg_match('@^--?[a-zA-Z0-9_-]+@', $arg)) {
|
|
||||||
return $arg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getTagShort (): ?string {
|
|
||||||
return $this->tagShort;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit le tag court.
|
|
||||||
*
|
|
||||||
* @param string|null $tagShort Le tage court.
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*
|
|
||||||
* @see $tagShort
|
|
||||||
*/
|
|
||||||
public function setTagShort (string $tagShort = null): self {
|
|
||||||
$this->tagShort = $tagShort;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Est-ce que l'argument existe en forme courte ?
|
|
||||||
*
|
|
||||||
* @return boolean True si existe en forme courte, sinon False.
|
|
||||||
*/
|
|
||||||
public function hasTagShort (): bool {
|
|
||||||
return !is_null($this->tagShort);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getTagLong (): string {
|
|
||||||
return $this->tagLong;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit le tag long.
|
|
||||||
*
|
|
||||||
* Si non fourni, est déduit du {@see $name nom de l'argument} :
|
|
||||||
* - Les "_" sont remplacés par "-"
|
|
||||||
* - Les lettres majuscules sont remplacées par "-" suivit de la lettre en minuscule
|
|
||||||
*
|
|
||||||
* @param string|null $tagLong Le tag long.
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*
|
|
||||||
* @see $tagLong
|
|
||||||
*/
|
|
||||||
public function setTagLong (string $tagLong = null): self {
|
|
||||||
if (is_null($tagLong)) {
|
|
||||||
$this->tagLong = preg_replace_callback(
|
|
||||||
'/[A-Z_]/',
|
|
||||||
function ($matches) {
|
|
||||||
if ($matches[0] == '_') {
|
|
||||||
return '-';
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return '-' . strtolower($matches[0]);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
$this->getName()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$this->tagLong = $tagLong;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function allowMultiple (): bool {
|
|
||||||
return $this->multiple;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit si l'argument est autorisé plusieurs fois ou non.
|
|
||||||
*
|
|
||||||
* @param boolean $multiple Argument autorisé plusieurs fois ?
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
* @see $_optional
|
|
||||||
*/
|
|
||||||
public function setMultiple (bool $multiple): self {
|
|
||||||
if (!is_bool($multiple)) {
|
|
||||||
throw new InvalidArgumentException('La multiplicité n\'est pas un booléen');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->multiple = $multiple;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isStoppingParse (): bool {
|
|
||||||
return $this->isStoppingParse;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit si l'argument met fin au parsage ou non.
|
|
||||||
*
|
|
||||||
* @param boolean $stoppingParse Met fin au parsage ?
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
* @see $_optional
|
|
||||||
*/
|
|
||||||
public function setStoppingParse (bool $stoppingParse): self {
|
|
||||||
if (!is_bool($stoppingParse)) {
|
|
||||||
throw new InvalidArgumentException('La stoppabilité n\'est pas un booléen');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->isStoppingParse = $stoppingParse;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine\Argument\Option;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use jrosset\CommandLine\Argument\AbstractArgumentList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of value arguments
|
||||||
|
*/
|
||||||
|
class OptionArgumentList extends AbstractArgumentList {
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
protected function checkArrayElementType ($key, $element): void {
|
||||||
|
if (!$element instanceof IOptionArgument) {
|
||||||
|
throw new InvalidArgumentException('The "' . $key . '" element is not a ' . IOptionArgument::class . ' instance');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,91 +1,158 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
|
||||||
* Déclaration de la classe CommandLine\Argument\Option\Value
|
|
||||||
*/
|
|
||||||
namespace jrosset\CommandLine\Argument\Option;
|
namespace jrosset\CommandLine\Argument\Option;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
use jrosset\CommandLine\Argument\IArgumentValueDescription;
|
use jrosset\CommandLine\Argument\IArgumentValueDescription;
|
||||||
use jrosset\CommandLine\Argument\Parser\IValueParser;
|
use jrosset\CommandLine\Argument\Parser\IParser;
|
||||||
use jrosset\CommandLine\Argument\ParseResult;
|
use UnexpectedValueException;
|
||||||
use jrosset\CommandLine\Exception\IncorrectParse;
|
|
||||||
use RuntimeException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Option contenant une valeur
|
* Option argument with possible values
|
||||||
*
|
|
||||||
* @package CommandLine\Argument\Option
|
|
||||||
*/
|
*/
|
||||||
class Value extends OptionAbstract implements IArgumentValueDescription {
|
class Value extends AbstractOptionArgument implements IArgumentValueDescription {
|
||||||
|
/**
|
||||||
|
* @var IParser The values parser
|
||||||
|
*/
|
||||||
|
private IParser $valuesParser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int The minimum number of values
|
||||||
|
*/
|
||||||
|
private int $valueOccurMin;
|
||||||
/**
|
/**
|
||||||
* @var IValueParser Parseur pour la valeur
|
* @var int|null The maximum number of values. Null if unlimited
|
||||||
*/
|
*/
|
||||||
protected IValueParser $valueParser;
|
private ?int $valueOccurMax;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Crée un nouvel argument de type option
|
* Create a new option argument
|
||||||
*
|
*
|
||||||
* @param string $name Le nom
|
* By default, the {@see AbstractArgument::$varName variable name} is the same as the {@see AbstractArgument::$name argument in help}
|
||||||
* @param string|null $description La description
|
*
|
||||||
* @param IValueParser $valueParser Le parseur de valeur
|
* @param string $name The argument name in help
|
||||||
* @param string|null $tagLong Le tag long
|
* @param string|null $description The argument name in documentation
|
||||||
* @param string|null $tagShort Le tag court
|
* @param IParser $valuesParser The value parser
|
||||||
|
* @param string|null $tagLong The long tag. If null, deduce from argument name <b>$name</b> (see {@see AbstractOptionArgument::convertNameToTagLong()}
|
||||||
|
* @param string|null $tagShort The short tag
|
||||||
*/
|
*/
|
||||||
public function __construct (string $name, ?string $description, IValueParser $valueParser, ?string $tagLong = null, ?string $tagShort = null) {
|
public function __construct (string $name, ?string $description, IParser $valuesParser, ?string $tagLong = null, ?string $tagShort = null) {
|
||||||
parent::__construct($name, $description, $tagLong, $tagShort);
|
parent::__construct($name, $description, $tagLong, $tagShort);
|
||||||
$this->setValueParser($valueParser);
|
|
||||||
|
$this->setValuesParser($valuesParser);
|
||||||
|
$this->setValueOccurMin(1);
|
||||||
|
$this->setValueOccurMax(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
public function parse (array $args): ?ParseResult {
|
public function getValueDescription (): string {
|
||||||
try {
|
return $this->valuesParser->getValueDescription();
|
||||||
if (!$this->_parseTag($args[0])) {
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($args) < 2 || is_null($args[1])) {
|
|
||||||
throw new IncorrectParse('La seconde valeur de l\'argument manquante');
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ParseResult($this->valueParser->parseValue($args[1]), 2);
|
/**
|
||||||
}
|
* The values parser
|
||||||
catch (RuntimeException $e) {
|
*
|
||||||
throw new IncorrectParse('Échec du parsage de la valeur "' . $args[0] . '"', 0, $e);
|
* @return IParser The values parser
|
||||||
}
|
*/
|
||||||
|
public function getValuesParser (): IParser {
|
||||||
|
return $this->valuesParser;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the values parser
|
||||||
|
*
|
||||||
|
* @param IParser $valuesParser The values parser
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setValuesParser (IParser $valuesParser): self {
|
||||||
|
$this->valuesParser = $valuesParser;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* Is the value optional ?
|
||||||
|
*
|
||||||
|
* @return bool Is the value optional ?
|
||||||
*/
|
*/
|
||||||
public function getValueDescription (): string {
|
public function isValueOptional (): bool {
|
||||||
return $this->valueParser->getValueDescription();
|
return $this->getValueOccurMin() === 0;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set if the value is optional
|
||||||
|
*
|
||||||
|
* @param bool $optional Is the value optional ?
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setValueOptional (bool $optional): self {
|
||||||
|
$this->setValueOccurMin($optional ? 0 : 1);
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Le parseur de valeur.
|
* The minimum number of values
|
||||||
*
|
*
|
||||||
* @return IValueParser Le parseur.
|
* @return int The minimum number of values
|
||||||
|
*/
|
||||||
|
public function getValueOccurMin (): int {
|
||||||
|
return $this->valueOccurMin;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the minimum number of values
|
||||||
|
*
|
||||||
|
* @param int $valueOccurMin The minimum number of values
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
*
|
*
|
||||||
* @see $valueParser
|
* @throws InvalidArgumentException If the minimum number of values provided is not valid (strictly positive integer)
|
||||||
|
* @throws UnexpectedValueException If the minimum number of values provided is strictly greater than the maximum
|
||||||
|
*/
|
||||||
|
public function setValueOccurMin (int $valueOccurMin): self {
|
||||||
|
if (!is_numeric($valueOccurMin) || floor($valueOccurMin) != $valueOccurMin || ($valueOccurMin = (int)$valueOccurMin) < 0) {
|
||||||
|
throw new InvalidArgumentException('The minimum number of values must be a strictly positive integer');
|
||||||
|
}
|
||||||
|
|
||||||
|
$valueOccurMax = $this->getValueOccurMax();
|
||||||
|
if ($valueOccurMax !== null && $valueOccurMin > $valueOccurMax) {
|
||||||
|
throw new UnexpectedValueException('The minimum number of values provided is strictly greater than the maximum');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->valueOccurMin = $valueOccurMin;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum number of values. Null if unlimited
|
||||||
*
|
*
|
||||||
* @noinspection PhpUnused
|
* @return int|null The maximum number of value
|
||||||
*/
|
*/
|
||||||
public function getValueParser (): IValueParser {
|
public function getValueOccurMax (): ?int {
|
||||||
return $this->valueParser;
|
return $this->valueOccurMax;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Définit le parseur de valeur
|
* Set the maximum number of values. Null if unlimited
|
||||||
*
|
*
|
||||||
* @param IValueParser $valueParser Le parseur
|
* @param int|null $valueOccurMax The maximum number of values
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*
|
*
|
||||||
* @see $valueParser
|
* @throws InvalidArgumentException If the maximum number of values provided is not valid (strictly positive integer)
|
||||||
|
* @throws UnexpectedValueException If the maximum number of values provided is strictly lower than the minimum
|
||||||
*/
|
*/
|
||||||
public function setValueParser (IValueParser $valueParser): self {
|
public function setValueOccurMax (?int $valueOccurMax): self {
|
||||||
$this->valueParser = $valueParser;
|
if ($valueOccurMax !== null) {
|
||||||
|
if (!is_numeric($valueOccurMax) || floor($valueOccurMax) != $valueOccurMax || ($valueOccurMax = (int)$valueOccurMax) < 0) {
|
||||||
|
throw new InvalidArgumentException('The maximum number of values must be a strictly positive integer');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($valueOccurMax < $this->getValueOccurMin()) {
|
||||||
|
throw new UnexpectedValueException('The maximum number of values is strictly lower than the minimum');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->valueOccurMax = $valueOccurMax;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,80 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Déclaration de la classe CommandLine\Argument\ParseResult.
|
|
||||||
*/
|
|
||||||
namespace jrosset\CommandLine\Argument;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Résultat du parsage d'un argument
|
|
||||||
*
|
|
||||||
* @package CommandLine\Argument
|
|
||||||
*/
|
|
||||||
class ParseResult {
|
|
||||||
/**
|
|
||||||
* @var int Le nombre d'argument consumé.
|
|
||||||
*/
|
|
||||||
protected int $consume;
|
|
||||||
/**
|
|
||||||
* @var mixed La valeur.
|
|
||||||
*/
|
|
||||||
protected $value;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ArgumentParseResult constructor.
|
|
||||||
*
|
|
||||||
* @param mixed $value La valeur.
|
|
||||||
* @param int $consume Le nombre d'argument consumé.
|
|
||||||
*/
|
|
||||||
public function __construct ($value, int $consume = 1) {
|
|
||||||
$this->setValue($value);
|
|
||||||
$this->setConsume($consume);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Le nombre d'argument consumé.
|
|
||||||
*
|
|
||||||
* @return int Le nombre d'argument consumé.
|
|
||||||
*
|
|
||||||
* @see $consume
|
|
||||||
*/
|
|
||||||
public function getConsume (): int {
|
|
||||||
return $this->consume;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit le nombre d'argument consumé.
|
|
||||||
*
|
|
||||||
* @param $consume int Le nombre d'argument consumé.
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*
|
|
||||||
* @see $consume
|
|
||||||
*/
|
|
||||||
public function setConsume (int $consume): self {
|
|
||||||
$this->consume = $consume;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La valeur.
|
|
||||||
*
|
|
||||||
* @return mixed La valeur.
|
|
||||||
*
|
|
||||||
* @see $value
|
|
||||||
*/
|
|
||||||
public function getValue() {
|
|
||||||
return $this->value;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit les valeurs définies.
|
|
||||||
*
|
|
||||||
* @param mixed $value La valeur.
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*
|
|
||||||
* @see $value
|
|
||||||
*/
|
|
||||||
public function setValue ($value): self {
|
|
||||||
$this->value = $value;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,117 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine\Argument\Value;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use jrosset\CommandLine\Argument\AbstractArgument;
|
||||||
|
use UnexpectedValueException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract base for value arguments
|
||||||
|
*/
|
||||||
|
abstract class AbstractValueArgument extends AbstractArgument implements IValueArgument {
|
||||||
|
/**
|
||||||
|
* @var int The minimum number of values
|
||||||
|
*/
|
||||||
|
protected int $occurMin = 1;
|
||||||
|
/**
|
||||||
|
* @var int|null The maximum number of values. Null if unlimited
|
||||||
|
*/
|
||||||
|
protected ?int $occurMax = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new value argument
|
||||||
|
*
|
||||||
|
* @param string $name The argument name in help
|
||||||
|
* @param string|null $description The argument description in help
|
||||||
|
* @param boolean $optional Optional argument ?
|
||||||
|
* @param mixed $default The default value
|
||||||
|
*/
|
||||||
|
protected function __construct (string $name, ?string $description, bool $optional = false, $default = null) {
|
||||||
|
parent::__construct($name, $description);
|
||||||
|
|
||||||
|
$this->setOptional($optional);
|
||||||
|
$this->setDefault($default);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is the argument optional ?
|
||||||
|
*
|
||||||
|
* @return bool Is the argument optional ?
|
||||||
|
*/
|
||||||
|
public function isOptional (): bool {
|
||||||
|
return $this->getOccurMin() === 0;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set if the argument is optional
|
||||||
|
*
|
||||||
|
* @param bool $optional Is the argument optional ?
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setOptional (bool $optional): self {
|
||||||
|
$this->setOccurMin($optional ? 0 : 1);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getOccurMin (): int {
|
||||||
|
return $this->occurMin;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the minimum number of occurrence of values
|
||||||
|
*
|
||||||
|
* @param int $occurMin The minimum number of occurrence of values
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException If the minimum number of occurrence of values provided is not valid (strictly positive integer)
|
||||||
|
* @throws UnexpectedValueException If the minimum number of occurrence of values provided is strictly greater than the maximum
|
||||||
|
*/
|
||||||
|
public function setOccurMin (int $occurMin): self {
|
||||||
|
if (!is_numeric($occurMin) || floor($occurMin) != $occurMin || ($occurMin = (int)$occurMin) < 0) {
|
||||||
|
throw new InvalidArgumentException('The minimum number of values must be a strictly positive integer');
|
||||||
|
}
|
||||||
|
|
||||||
|
$occurMax = $this->getOccurMax();
|
||||||
|
if ($occurMax !== null && $occurMin > $occurMax) {
|
||||||
|
throw new UnexpectedValueException('The minimum number of values provided is strictly greater than the maximum');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->occurMin = $occurMin;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getOccurMax (): ?int {
|
||||||
|
return $this->occurMax;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the maximum number of occurrence of values. Null if unlimited
|
||||||
|
*
|
||||||
|
* @param int|null $occurMax The maximum number of occurrence of values
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException If the maximum number of occurrence of values provided is not valid (strictly positive integer)
|
||||||
|
* @throws UnexpectedValueException If the maximum number of occurrence of values provided is strictly lower than the minimum
|
||||||
|
*/
|
||||||
|
public function setOccurMax (?int $occurMax): self {
|
||||||
|
if ($occurMax !== null) {
|
||||||
|
if (!is_numeric($occurMax) || floor($occurMax) != $occurMax || ($occurMax = (int)$occurMax) < 0) {
|
||||||
|
throw new InvalidArgumentException('The maximum number of values must be a strictly positive integer');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($occurMax < $this->getOccurMin()) {
|
||||||
|
throw new UnexpectedValueException('The maximum number of values is strictly lower than the minimum');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->occurMax = $occurMax;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
@ -1,77 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Déclaration de CommandLine\Argument\Value\FixedValue
|
|
||||||
*/
|
|
||||||
namespace jrosset\CommandLine\Argument\Value;
|
|
||||||
|
|
||||||
use jrosset\CommandLine\Argument\IArgumentValueDescription;
|
|
||||||
use jrosset\CommandLine\Argument\ParseResult;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Argument devant correspondre une valeur fixe
|
|
||||||
*
|
|
||||||
* @package CommandLine\Argument
|
|
||||||
*
|
|
||||||
* @noinspection PhpUnused
|
|
||||||
*/
|
|
||||||
class FixedValue extends ValueAbstract implements IArgumentValueDescription {
|
|
||||||
/**
|
|
||||||
* @var string La valeur que doit avoir l'argument
|
|
||||||
*/
|
|
||||||
protected string $value;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Crée un nouvel argument de type valeur
|
|
||||||
*
|
|
||||||
* @param string $varName Le nom de la variable
|
|
||||||
* @param string $value La valeur
|
|
||||||
* @param string|null $description La description
|
|
||||||
* @param boolean $optional Valeur optionnelle ?
|
|
||||||
*/
|
|
||||||
public function __construct (string $varName, string $value, ?string $description, bool $optional) {
|
|
||||||
parent::__construct($value, $description, $optional);
|
|
||||||
$this->setVarName($varName);
|
|
||||||
$this->setValue($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function parse (array $args): ?ParseResult {
|
|
||||||
if ($args[0] == $this->getValue()) {
|
|
||||||
return new ParseResult($this->getValue(), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function getValueDescription (): string {
|
|
||||||
return '"' . $this->getValue() . '"';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* La valeur.
|
|
||||||
*
|
|
||||||
* @return string La valeur.
|
|
||||||
*
|
|
||||||
* @see $value
|
|
||||||
*/
|
|
||||||
public function getValue (): string {
|
|
||||||
return $this->value;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit la valeur
|
|
||||||
*
|
|
||||||
* @param string $value La valeur
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
* @see $value
|
|
||||||
*/
|
|
||||||
public function setValue (string $value): self {
|
|
||||||
$this->value = $value;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Déclaration de l'interface CommandLine\Argument\Option\IArgumentValue
|
|
||||||
*/
|
|
||||||
namespace jrosset\CommandLine\Argument\Value;
|
|
||||||
|
|
||||||
use jrosset\CommandLine\Argument\IArgument;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface à implémenter si l'argument est de type "valeur"
|
|
||||||
*
|
|
||||||
* @package CommandLine\Argument\value
|
|
||||||
*/
|
|
||||||
interface IArgumentValue extends IArgument {
|
|
||||||
/**
|
|
||||||
* Le nombre minimum d'occurence.
|
|
||||||
*
|
|
||||||
* Généralement 0 (falcultatif) ou 1 (obligatoire).
|
|
||||||
*
|
|
||||||
* @return int Le nombre minimum d'occurence
|
|
||||||
*/
|
|
||||||
public function getOccurMin (): int;
|
|
||||||
/**
|
|
||||||
* Le nombre maximum d'occurence.
|
|
||||||
*
|
|
||||||
* Généralement 1 ou Null (illimité).
|
|
||||||
*
|
|
||||||
* @return int|null Le nombre maximum d'occurence
|
|
||||||
*/
|
|
||||||
public function getOccurMax (): ?int;
|
|
||||||
}
|
|
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine\Argument\Value;
|
||||||
|
|
||||||
|
use jrosset\CommandLine\Argument\IArgument;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface of value arguments
|
||||||
|
*/
|
||||||
|
interface IValueArgument extends IArgument {
|
||||||
|
/**
|
||||||
|
* The minimum number of occurrence of argument
|
||||||
|
*
|
||||||
|
* @return int The minimum number of occurrence of values
|
||||||
|
*/
|
||||||
|
public function getOccurMin (): int;
|
||||||
|
/**
|
||||||
|
* The maximum number of occurrence of values. Null if unlimited
|
||||||
|
*
|
||||||
|
* @return int|null The maximum of occurrence number of value
|
||||||
|
*/
|
||||||
|
public function getOccurMax (): ?int;
|
||||||
|
}
|
@ -1,82 +1,61 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Déclaration de CommandLine\Argument\Value\Value
|
|
||||||
*/
|
|
||||||
namespace jrosset\CommandLine\Argument\Value;
|
namespace jrosset\CommandLine\Argument\Value;
|
||||||
|
|
||||||
use jrosset\CommandLine\Argument\IArgumentValueDescription;
|
use jrosset\CommandLine\Argument\IArgumentValueDescription;
|
||||||
use jrosset\CommandLine\Argument\Parser\IValueParser;
|
use jrosset\CommandLine\Argument\Parser\IParser;
|
||||||
use jrosset\CommandLine\Argument\ParseResult;
|
|
||||||
use jrosset\CommandLine\Exception\IncorrectParse;
|
|
||||||
use RangeException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Argument de type "valeur"
|
* Argument de type "valeur"
|
||||||
*
|
*
|
||||||
* @package CommandLine\Argument\Value
|
* @package CommandLine\Argument\Value
|
||||||
*/
|
*/
|
||||||
class Value extends ValueAbstract implements IArgumentValueDescription {
|
class Value extends AbstractValueArgument implements IArgumentValueDescription {
|
||||||
/**
|
/**
|
||||||
* @var IValueParser Parseur pour la valeur
|
* @var IParser The values parser
|
||||||
*/
|
*/
|
||||||
protected IValueParser $valueParser;
|
private IParser $valuesParser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Crée un nouvel argument de type valeur
|
* Create a new value argument
|
||||||
*
|
*
|
||||||
* @param string $name Le nom
|
* By default, the {@see AbstractArgument::$varName variable name} is the same as the {@see AbstractArgument::$name argument in help}
|
||||||
* @param string|null $description La description
|
*
|
||||||
* @param IValueParser $valueParser Le parseur de valeur.
|
* @param string $name The argument name in help
|
||||||
* @param boolean $optional Valeur optionnelle ?
|
* @param string|null $description The argument name in documentation
|
||||||
*/
|
* @param IParser $valuesParser The value parser
|
||||||
public function __construct (string $name, ?string $description, IValueParser $valueParser, bool $optional) {
|
* @param boolean $optional Optional argument ?
|
||||||
parent::__construct($name, $description, $optional);
|
* @param mixed $default The default value
|
||||||
|
|
||||||
$this->setValueParser($valueParser);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
*/
|
||||||
public function parse (array $args): ?ParseResult {
|
public function __construct (string $name, ?string $description, IParser $valuesParser, bool $optional = false, $default = null) {
|
||||||
try {
|
parent::__construct($name, $description, $optional, $default);
|
||||||
return new ParseResult($this->valueParser->parseValue($args[0]), 1);
|
$this->setValuesParser($valuesParser);
|
||||||
}
|
|
||||||
catch (RangeException $e) {
|
|
||||||
throw new IncorrectParse('Échec du parsage de la valeur "' . $args[0] . '"', 0, $e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
public function getValueDescription (): string {
|
public function getValueDescription (): string {
|
||||||
return $this->valueParser->getValueDescription();
|
return $this->valuesParser->getValueDescription();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Le parseur de valeur.
|
* The values parser
|
||||||
*
|
*
|
||||||
* @return IValueParser Le parseur.
|
* @return IParser The values parser
|
||||||
*
|
|
||||||
* @see $valueParser
|
|
||||||
*
|
|
||||||
* @noinspection PhpUnused
|
|
||||||
*/
|
*/
|
||||||
public function getValueParser (): IValueParser {
|
public function getValuesParser (): IParser {
|
||||||
return $this->valueParser;
|
return $this->valuesParser;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Définit le parseur de valeur
|
* Set the values parser
|
||||||
*
|
*
|
||||||
* @param IValueParser $valueParser Le parseur
|
* @param IParser $valuesParser The values parser
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*
|
|
||||||
* @see $valueParser
|
|
||||||
*/
|
*/
|
||||||
public function setValueParser (IValueParser $valueParser): self {
|
public function setValuesParser (IParser $valuesParser): self {
|
||||||
$this->valueParser = $valueParser;
|
$this->valuesParser = $valuesParser;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,127 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Déclaration de la classe CommandLine\Argument\Value\ValueArgument.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace jrosset\CommandLine\Argument\Value;
|
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use jrosset\CommandLine\Argument\ArgumentAbstract;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Classe abstraite pour les paramètres de type "valeur"
|
|
||||||
*
|
|
||||||
* @package CommandLine\Argument\Value
|
|
||||||
*/
|
|
||||||
abstract class ValueAbstract extends ArgumentAbstract implements IArgumentValue {
|
|
||||||
/**
|
|
||||||
* @var int Le nombre minimum d'occurence.
|
|
||||||
*/
|
|
||||||
protected int $occurMin = 1;
|
|
||||||
/**
|
|
||||||
* @var int|null Le nombre maximum d'occurence. Null si illimité
|
|
||||||
*/
|
|
||||||
protected ?int $occurMax = 1;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Crée un nouvel argument.
|
|
||||||
*
|
|
||||||
* @param string $name Le nom de l'argument
|
|
||||||
* @param string|null $description La description de l'argument
|
|
||||||
* @param boolean $optional Argument optionel ?
|
|
||||||
*/
|
|
||||||
protected function __construct (string $name, ?string $description, bool $optional) {
|
|
||||||
parent::__construct($name, $description);
|
|
||||||
|
|
||||||
$this->setOptional($optional);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Est-ce que l'argument est facultatif ?
|
|
||||||
*
|
|
||||||
* @return bool Est-ce que l'argument est facultatif ?
|
|
||||||
*
|
|
||||||
* @noinspection PhpUnused
|
|
||||||
*/
|
|
||||||
public function isOptional (): bool {
|
|
||||||
return $this->getOccurMin() === 0;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit si l'argument est facultatif ou non.
|
|
||||||
*
|
|
||||||
* @param bool $optional Argument facultatif ?
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setOptional (bool $optional): self {
|
|
||||||
$this->setOccurMin($optional ? 0 : 1);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function getOccurMin (): int {
|
|
||||||
return $this->occurMin;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit le nombre minimum d'occurence.
|
|
||||||
*
|
|
||||||
* @param int $occurMin Le nombre minimum
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
* @see $occurMin
|
|
||||||
*/
|
|
||||||
public function setOccurMin (int $occurMin): self {
|
|
||||||
if (!is_numeric($occurMin)) {
|
|
||||||
throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (floor($occurMin) != $occurMin) {
|
|
||||||
throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier');
|
|
||||||
}
|
|
||||||
|
|
||||||
$int = (int)$occurMin;
|
|
||||||
if ($int < 0) {
|
|
||||||
throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier positif');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->occurMin = $int;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function getOccurMax (): ?int {
|
|
||||||
return $this->occurMax;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Définit le nombre maximum d'occurence.
|
|
||||||
*
|
|
||||||
* @param int|null $occurMax Le nombre maximum. Null si illimité
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
* @see $occurMax
|
|
||||||
*/
|
|
||||||
public function setOccurMax (?int $occurMax): self {
|
|
||||||
if (!is_null($occurMax)) {
|
|
||||||
if (!is_numeric($occurMax)) {
|
|
||||||
throw new InvalidArgumentException('Le nombre maximum d\'ocurrence n\'est pas un entier');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (floor($occurMax) != $occurMax) {
|
|
||||||
throw new InvalidArgumentException('Le nombre maximum d\'ocurrence n\'est pas un entier');
|
|
||||||
}
|
|
||||||
|
|
||||||
$occurMax = (int)$occurMax;
|
|
||||||
if ($occurMax <= 0) {
|
|
||||||
throw new InvalidArgumentException('Le nombre maximum d\'ocurrence n\'est pas un entier strictement positif');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->occurMax = $occurMax;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine\Argument\Value;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use jrosset\CommandLine\Argument\AbstractArgumentList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of value arguments
|
||||||
|
*/
|
||||||
|
class ValueArgumentList extends AbstractArgumentList {
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
protected function checkArrayElementType ($key, $element): void {
|
||||||
|
if (!$element instanceof IValueArgument) {
|
||||||
|
throw new InvalidArgumentException('The "' . $key . '" element is not a ' . IValueArgument::class . ' instance');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,130 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An object-oriented class for an array
|
||||||
|
*
|
||||||
|
* @template T
|
||||||
|
*/
|
||||||
|
class ArrayClass {
|
||||||
|
/**
|
||||||
|
* @var T[] The internal array
|
||||||
|
*/
|
||||||
|
private array $array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an empty array
|
||||||
|
*/
|
||||||
|
public function __construct () {
|
||||||
|
$this->setAll([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all elements of the array
|
||||||
|
*
|
||||||
|
* @return T[] all elements of the array
|
||||||
|
*/
|
||||||
|
public function getAll (): array {
|
||||||
|
return $this->array;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* <b>Replace</b> all elements of the array
|
||||||
|
*
|
||||||
|
* @param T[] $array The new list of elements
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException If an element of the array is not valid
|
||||||
|
*
|
||||||
|
* @see ArrayClass::add()
|
||||||
|
*/
|
||||||
|
public function setAll (array $array): self {
|
||||||
|
self::checkArrayType($array);
|
||||||
|
|
||||||
|
$this->array = [];
|
||||||
|
foreach ($array as $element) {
|
||||||
|
$this->add($element);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add one or an array of elements
|
||||||
|
*
|
||||||
|
* @param T|T[] $elements The element(s) to add
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException If an element of the array is not valid
|
||||||
|
*/
|
||||||
|
public function add ($elements): self {
|
||||||
|
if (!is_array($elements)) {
|
||||||
|
$elements = [$elements];
|
||||||
|
}
|
||||||
|
$this->checkArrayType($elements);
|
||||||
|
|
||||||
|
$realArray = [];
|
||||||
|
foreach ($elements as $element) {
|
||||||
|
$key = $this->getKeyForElement($element);
|
||||||
|
if ($key === null) {
|
||||||
|
$realArray[] = $element;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$realArray[$key] = $element;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->array = array_merge($this->array, $realArray);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a element exists from his key
|
||||||
|
*
|
||||||
|
* @param int|string $elementKey The element key to check
|
||||||
|
*
|
||||||
|
* @return bool True if the element exists, else False
|
||||||
|
*/
|
||||||
|
public function exists ($elementKey): bool {
|
||||||
|
return isset($this->getAll()[$elementKey]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if all elements of the parameter array are valid
|
||||||
|
*
|
||||||
|
* @param T[] $array The array to check
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException If an element of the array is not valid
|
||||||
|
*
|
||||||
|
* @see ArrayClass::$checker
|
||||||
|
*/
|
||||||
|
protected function checkArrayType (array $array): void {
|
||||||
|
foreach ($array as $arrayKey => $arrayElement) {
|
||||||
|
$this->checkArrayElementType($arrayKey, $arrayElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Check if the element of the array is valid
|
||||||
|
*
|
||||||
|
* @param int|string $key The key of the element
|
||||||
|
* @param T $element The element to check
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException If the element is not valid
|
||||||
|
*/
|
||||||
|
protected function checkArrayElementType ($key, $element): void {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the key of an element. Null if auto-generated
|
||||||
|
*
|
||||||
|
* @param T $element The element
|
||||||
|
*
|
||||||
|
* @return int|string|null The key or Null
|
||||||
|
* @noinspection PhpReturnDocTypeMismatchInspection
|
||||||
|
*/
|
||||||
|
protected function getKeyForElement ($element) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine\ReturnCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for a return code
|
||||||
|
*/
|
||||||
|
interface IReturnCode {
|
||||||
|
/**
|
||||||
|
* The returned code
|
||||||
|
*
|
||||||
|
* @return int The returned code
|
||||||
|
*/
|
||||||
|
public function getCode (): int;
|
||||||
|
/**
|
||||||
|
* The description of the return code
|
||||||
|
*
|
||||||
|
* @return string|null The description of the return code
|
||||||
|
*/
|
||||||
|
public function getDescription (): ?string;
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine\ReturnCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A return code
|
||||||
|
*/
|
||||||
|
class ReturnCode implements IReturnCode {
|
||||||
|
/**
|
||||||
|
* @var int The returned code
|
||||||
|
*/
|
||||||
|
private int $code;
|
||||||
|
/**
|
||||||
|
* @var string|null The description of the return code
|
||||||
|
*/
|
||||||
|
private ?string $description;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new return code
|
||||||
|
*
|
||||||
|
* @param int $code The returned code
|
||||||
|
* @param string|null $description The description of the return code
|
||||||
|
*/
|
||||||
|
public function __construct (int $code, ?string $description = null) {
|
||||||
|
$this->setCode($code);
|
||||||
|
$this->setDescription($description);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getCode (): int {
|
||||||
|
return $this->code;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the returned code
|
||||||
|
*
|
||||||
|
* @param int $code The returned code
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setCode (int $code): self {
|
||||||
|
$this->code = $code;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getDescription (): ?string {
|
||||||
|
return $this->description;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the description of the return code
|
||||||
|
*
|
||||||
|
* @param string|null $description The description of the return code
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setDescription (?string $description): self {
|
||||||
|
$this->description = $description;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CommandLine\ReturnCode;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use jrosset\CommandLine\ArrayClass;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of return codes
|
||||||
|
*
|
||||||
|
* @extends ArrayClass<IReturnCode>
|
||||||
|
*/
|
||||||
|
class ReturnCodeList extends ArrayClass {
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
protected function checkArrayElementType ($key, $element): void {
|
||||||
|
if (!$element instanceof IReturnCode) {
|
||||||
|
throw new InvalidArgumentException('The "' . $key . '" element is not a ' . IReturnCode::class . ' instance');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the key of an element. Null if auto-generated
|
||||||
|
*
|
||||||
|
* @param IReturnCode $element The element
|
||||||
|
*
|
||||||
|
* @return int The key or Null
|
||||||
|
*/
|
||||||
|
protected function getKeyForElement ($element): int {
|
||||||
|
return $element->getCode();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue