Code reorganization (PSR-4)

master
Julien Rosset 6 years ago
parent f950043b9c
commit 0742795afa

@ -64,6 +64,23 @@ abstract class OptionAbstract extends ArgumentAbstract implements IArgumentOptio
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($args) {
foreach ($args as $arg) {
if (preg_match('@^--?[a-zA-Z0-9_-]+@', $arg)) {
return (string)$arg;
}
}
return false;
}
public function getTagShort() {
return $this->_tagShort;
}

@ -7,7 +7,7 @@ namespace CommandLine\Argument\Parser;
use RangeException;
/**
* Parseur vers un booléen
* Parser vers un booléen
*
* Les valeurs suivantes sont acceptées :
* - true , 1, oui, vrai

@ -89,10 +89,14 @@ class DecimalParser implements IValueParser {
* @return $this
*/
public function setValueMin($valueMin = null) {
if(!is_null($valueMin) && !$this->_isDecimal($valueMin))
throw new InvalidArgumentException('La valeur n\'est pas un entier ou null' . $valueMin);
if (!is_null($valueMin)) {
if (!$this->_isDecimal($valueMin)) {
throw new InvalidArgumentException('Le minimum n\'est pas un entier ou null : ' . $valueMin);
}
$valueMin = (double)$valueMin;
}
$this->_valueMin = (int)$valueMin;
$this->_valueMin = $valueMin;
return $this;
}
/**
@ -122,10 +126,14 @@ class DecimalParser implements IValueParser {
* @return $this
*/
public function setValueMax($valueMax = null) {
if(!is_null($valueMax) && !$this->_isDecimal($valueMax))
throw new InvalidArgumentException('La valeur n\'est pas un entier ou null : ' . $valueMax);
if (!is_null($valueMax)) {
if (!$this->_isDecimal($valueMax)) {
throw new InvalidArgumentException('Le maximum n\'est pas un entier ou null : ' . $valueMax);
}
$valueMax = (double)$valueMax;
}
$this->_valueMax = (int)$valueMax;
$this->_valueMax = $valueMax;
return $this;
}
/**

@ -92,10 +92,14 @@ class IntegerParser implements IValueParser {
* @return $this
*/
public function setValueMin($valueMin = null) {
if(!is_null($valueMin) && !$this->_isInt($valueMin))
throw new InvalidArgumentException('La valeur n\'est pas un entier ou null : '.$valueMin);
if (!is_null($valueMin)) {
if (!$this->_isInt($valueMin)) {
throw new InvalidArgumentException('Le minimum n\'est pas un entier ou null : ' . $valueMin);
}
$valueMin = (int)$valueMin;
}
$this->_valueMin = (int)$valueMin;
$this->_valueMin = $valueMin;
return $this;
}
/**
@ -125,10 +129,14 @@ class IntegerParser implements IValueParser {
* @return $this
*/
public function setValueMax($valueMax = null) {
if(!is_null($valueMax) && !$this->_isInt($valueMax))
throw new InvalidArgumentException('La valeur n\'est pas un entier ou null : '.$valueMax);
if (!is_null($valueMax)) {
if (!$this->_isInt($valueMax)) {
throw new InvalidArgumentException('Le maximum n\'est pas un entier ou null : ' . $valueMax);
}
$valueMax = (int)$valueMax;
}
$this->_valueMax = (int)$valueMax;
$this->_valueMax = $valueMax;
return $this;
}
/**

@ -473,7 +473,7 @@ class CommandLine {
}
}
catch (ReflectionException $e) {
$type = '<inconnu>';
$type = /** @lang text */'<inconnu>';
}
throw new InvalidArgumentException('L\'argument n\'est pas d\'un type géré : ' . $type);
@ -561,11 +561,11 @@ class CommandLine {
* @param boolean $exitAtEnd Terminer le script à la fin de la fonction correspondante ?
*/
public function treatDefaultArguments ($values, $exitAtEnd = true) {
if ($values->{self::ARGUMENT_OPTION_HELP} === true) {
if (isset($values->{self::ARGUMENT_OPTION_HELP}) && $values->{self::ARGUMENT_OPTION_HELP} === true) {
$this->showHelp($exitAtEnd);
}
if ($values->{self::ARGUMENT_OPTION_VERSION} === true) {
if (isset($values->{self::ARGUMENT_OPTION_VERSION}) && $values->{self::ARGUMENT_OPTION_VERSION} === true) {
$this->showVersion($exitAtEnd);
}
}
@ -613,7 +613,7 @@ class CommandLine {
$help[] = '';
$syntax = array(
$this->getCommand(),
empty($this->getCommand()) ? $this->getProgramName() : $this->getCommand(),
count($this->getOptions()) > 0 ? '[OPTIONS]' : '',
);
$syntax = array_merge($syntax, array_map(array(__CLASS__, '_getSyntaxOfValue'), $this->getValues()));
@ -854,6 +854,9 @@ class CommandLine {
return $out;
}
if(($arg = OptionAbstract::containsOption($argv)) !== false)
throw new IncorrectParse('Option inconnue : ' . $arg);
$values = array_values($this->getValues());
/**
* @var int $ordre

@ -4,7 +4,6 @@
*/
namespace CommandLine\Exception;
use CommandLine\Solution;
use RuntimeException;
/**
Loading…
Cancel
Save