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.
phpcommandline/Argument/Option/OptionAbstract.class.php

164 lines
4.4 KiB
PHP

<?php
/**
* Déclaration de la classe CommandLine\Argument\Option\OptionAbstract.
*/
namespace CommandLine\Argument\Option;
use CommandLine\Argument\ArgumentAbstract;
use InvalidArgumentException;
/**
* 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 $_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
* @see $_optional
*/
public function setMultiple($multiple) {
if (!is_bool($multiple))
throw new InvalidArgumentException('La multiplicité 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
* @see $_optional
*/
public function setStoppingParse($stoppingParse) {
if (!is_bool($stoppingParse))
throw new InvalidArgumentException('La stoppabilité n\'est pas un booléen');
$this->_isStoppingParse = $stoppingParse;
return $this;
}
}