You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
5.1 KiB
PHP
177 lines
5.1 KiB
PHP
<?php
|
|
/**
|
|
* Déclaration de la classe CommandLine\Argument\Option\OptionAbstract.
|
|
*/
|
|
namespace CommandLine\Argument\Option;
|
|
|
|
use Fidit\v3\Exception\InvalidArgument;
|
|
use CommandLine\Argument\ArgumentAbstract;
|
|
|
|
/**
|
|
* Classe abstraite pour les arguments de type "option" : --xxx / -x
|
|
*
|
|
* NOTE : les arguments de type "option" ne peuvent autoriser plusieurs valeurs ({@see $default $default} = 1).
|
|
*
|
|
* @property string|null $tagShort {@see $_tagShort $_tagShort }
|
|
* @property string|null $tagLong {@see $_tagLong $_tagLong }
|
|
* @property boolean $multiple {@see $_multiple $_multiple }
|
|
* @property boolean $isStoppingParse {@see $_isStoppingParse $_isStoppingParse}
|
|
*
|
|
* @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 $_tagShort;
|
|
/**
|
|
* @var string L'argument long (cas d'un "--").
|
|
*/
|
|
protected $_tagLong;
|
|
|
|
/**
|
|
* @var boolean Est-ce que l'argument est autorisé plusieurs fois ?
|
|
*/
|
|
protected $_multiple = false;
|
|
/**
|
|
* @var boolean Est-ce que l'argument met fin au parsage ?
|
|
*/
|
|
protected $_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 ($name, $description, $tagLong = null, $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($arg) {
|
|
if($this->hasTagShort()) {
|
|
if ($arg == '-'.$this->getTagShort())
|
|
return true;
|
|
}
|
|
|
|
return $arg == '--'.$this->getTagLong();
|
|
}
|
|
|
|
public function getTagShort() {
|
|
return $this->_tagShort;
|
|
}
|
|
/**
|
|
* Définit le tag court.
|
|
*
|
|
* @param string|null $tagShort Le tage court.
|
|
*
|
|
* @return $this
|
|
*
|
|
* @see $_tagShort
|
|
*/
|
|
public function setTagShort($tagShort = null) {
|
|
$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() {
|
|
return !is_null($this->_tagShort);
|
|
}
|
|
|
|
public function getTagLong() {
|
|
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($tagLong = null) {
|
|
$this->_tagLong = $tagLong;
|
|
|
|
if(is_null($this->_tagLong)) {
|
|
$this->_tagLong = preg_replace_callback(
|
|
'/[A-Z_]/',
|
|
function ($matches) {
|
|
if($matches[0] == '_')
|
|
return '-';
|
|
else
|
|
return '-'.strtolower($matches[0]);
|
|
},
|
|
$this->getName()
|
|
);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function allowMultiple() {
|
|
return $this->_multiple;
|
|
}
|
|
/**
|
|
* Définit si l'argument est autorisé plusieurs fois ou non.
|
|
*
|
|
* @param boolean $multiple Argument autorisé plusieurs fois ?
|
|
*
|
|
* @return $this
|
|
*
|
|
* @throws InvalidArgument Si la valeur n'est pas un booléen
|
|
*
|
|
* @see $_optional
|
|
*/
|
|
public function setMultiple($multiple) {
|
|
if (!is_bool($multiple))
|
|
throw new InvalidArgument($multiple, 'La valeur n\'est pas un booléen');
|
|
|
|
$this->_multiple = $multiple;
|
|
return $this;
|
|
}
|
|
|
|
public function isStoppingParse() {
|
|
return $this->_isStoppingParse;
|
|
}
|
|
/**
|
|
* Définit si l'argument met fin au parsage ou non.
|
|
*
|
|
* @param boolean $stoppingParse Met fin au parsage ?
|
|
*
|
|
* @return $this
|
|
*
|
|
* @throws InvalidArgument Si la valeur n'est pas un booléen
|
|
*
|
|
* @see $_optional
|
|
*/
|
|
public function setStoppingParse($stoppingParse) {
|
|
if (!is_bool($stoppingParse))
|
|
throw new InvalidArgument($stoppingParse, 'La valeur n\'est pas un booléen');
|
|
|
|
$this->_isStoppingParse = $stoppingParse;
|
|
return $this;
|
|
}
|
|
} |