Code reorganization (PSR-4)

master
Julien Rosset 6 years ago
parent f950043b9c
commit 0742795afa

@ -1,19 +1,19 @@
<?php <?php
/** /**
* Déclaration de l'interface CommandLine\Argument\IArgumentValueDescription * Déclaration de l'interface CommandLine\Argument\IArgumentValueDescription
*/ */
namespace CommandLine\Argument; namespace CommandLine\Argument;
/** /**
* Interface à implementer par les arguments qui souhaitent détailler la valeur attendue * Interface à implementer par les arguments qui souhaitent détailler la valeur attendue
* *
* @package CommandLine\Argument * @package CommandLine\Argument
*/ */
interface IArgumentValueDescription { interface IArgumentValueDescription {
/** /**
* La description de la valeur de d'argument. * La description de la valeur de d'argument.
* *
* @return string La description. * @return string La description.
*/ */
public function getValueDescription(); public function getValueDescription();
} }

@ -1,48 +1,48 @@
<?php <?php
/** /**
* Déclare la classe CommandLine\Argument\Option\OptionFlagWithReverse. * Déclare la classe CommandLine\Argument\Option\OptionFlagWithReverse.
*/ */
namespace CommandLine\Argument\Option; namespace CommandLine\Argument\Option;
/** /**
* Option de type "flag" : vaut True ou False avec son tag inversé. * 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 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". * Le tag court inversé est fait de la même manière, mais avec le préfixe "n".
* *
* Exemples : * Exemples :
* - "--force" donne "--no-force" * - "--force" donne "--no-force"
* - "-f" donne "-nf" * - "-f" donne "-nf"
* - "--no-stop" devient "--stop" * - "--no-stop" devient "--stop"
* *
* @package CommandLine\Argument\Option * @package CommandLine\Argument\Option
*/ */
class FlagWithReverse extends Flag implements IArgumentOptionSecondary { class FlagWithReverse extends Flag implements IArgumentOptionSecondary {
public function getOthersOptions () { public function getOthersOptions () {
$tagShort = null; $tagShort = null;
if($this->hasTagShort()) { if($this->hasTagShort()) {
if(substr($this->getTagShort(), 0, 1) == 'n') if(substr($this->getTagShort(), 0, 1) == 'n')
$tagShort = substr($this->getTagShort(), 1); $tagShort = substr($this->getTagShort(), 1);
else else
$tagShort = 'n'.$this->getTagShort(); $tagShort = 'n'.$this->getTagShort();
} }
if(substr($this->getTagLong(), 0, 3) == 'no-') if(substr($this->getTagLong(), 0, 3) == 'no-')
$tagLong = substr($this->getTagLong(), 3); $tagLong = substr($this->getTagLong(), 3);
else else
$tagLong = 'no-'.$this->getTagLong(); $tagLong = 'no-'.$this->getTagLong();
$name = $this->getName(); $name = $this->getName();
if(substr($name, 0, 3) == 'no') if(substr($name, 0, 3) == 'no')
$name = strtolower(substr($name, 2, 1)).substr($name, 3); $name = strtolower(substr($name, 2, 1)).substr($name, 3);
else else
$name = 'no'.strtoupper(substr($name, 0, 1)).substr($name, 1); $name = 'no'.strtoupper(substr($name, 0, 1)).substr($name, 1);
$description = '[INVERSE] '.$this->getDescription(); $description = '[INVERSE] '.$this->getDescription();
$reverse = new Flag($name, !$this->getDefault(), $description, $tagLong, $tagShort); $reverse = new Flag($name, !$this->getDefault(), $description, $tagLong, $tagShort);
$reverse->setVarName($this->getVarName()); $reverse->setVarName($this->getVarName());
return array($reverse); return array($reverse);
} }
} }

@ -1,46 +1,46 @@
<?php <?php
/** /**
* Déclaration de l'interface CommandLine\Argument\Option\IArgumentOption * Déclaration de l'interface CommandLine\Argument\Option\IArgumentOption
*/ */
namespace CommandLine\Argument\Option; namespace CommandLine\Argument\Option;
use CommandLine\Argument\IArgument; use CommandLine\Argument\IArgument;
/** /**
* Interface à implémenter si l'argument est de type "option" * Interface à implémenter si l'argument est de type "option"
* *
* @package CommandLine\Argument\Option * @package CommandLine\Argument\Option
*/ */
interface IArgumentOption extends IArgument { interface IArgumentOption extends IArgument {
/** /**
* La tag court. * La tag court.
* *
* @return string|null Le tag court. * @return string|null Le tag court.
* *
* @see $_tagShort * @see $_tagShort
*/ */
public function getTagShort(); public function getTagShort();
/** /**
* Le tag long. * Le tag long.
* *
* @return string Le tag long. * @return string Le tag long.
* *
* @see $_tagLong * @see $_tagLong
*/ */
public function getTagLong(); public function getTagLong();
/** /**
* Est-ce que l'argument est autorisé plusieurs fois ? * Est-ce que l'argument est autorisé plusieurs fois ?
* *
* @return boolean True si l'argument est autorisé plusieurs fois, sinon False * @return boolean True si l'argument est autorisé plusieurs fois, sinon False
*/ */
public function allowMultiple(); public function allowMultiple();
/** /**
* Est-ce que l'argument met fin au parsage ? * Est-ce que l'argument met fin au parsage ?
* *
* Exemple : --help = affiche l'aide et stoppe le programme * 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. * @return boolean True si la présence de l'argument mat fin au parsage, sinon False.
*/ */
public function isStoppingParse(); public function isStoppingParse();
} }

@ -64,6 +64,23 @@ abstract class OptionAbstract extends ArgumentAbstract implements IArgumentOptio
return $arg == '--'.$this->getTagLong(); 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() { public function getTagShort() {
return $this->_tagShort; return $this->_tagShort;
} }

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

@ -89,10 +89,14 @@ class DecimalParser implements IValueParser {
* @return $this * @return $this
*/ */
public function setValueMin($valueMin = null) { public function setValueMin($valueMin = null) {
if(!is_null($valueMin) && !$this->_isDecimal($valueMin)) if (!is_null($valueMin)) {
throw new InvalidArgumentException('La valeur n\'est pas un entier ou 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; return $this;
} }
/** /**
@ -122,10 +126,14 @@ class DecimalParser implements IValueParser {
* @return $this * @return $this
*/ */
public function setValueMax($valueMax = null) { public function setValueMax($valueMax = null) {
if(!is_null($valueMax) && !$this->_isDecimal($valueMax)) if (!is_null($valueMax)) {
throw new InvalidArgumentException('La valeur n\'est pas un entier ou 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; return $this;
} }
/** /**

@ -92,10 +92,14 @@ class IntegerParser implements IValueParser {
* @return $this * @return $this
*/ */
public function setValueMin($valueMin = null) { public function setValueMin($valueMin = null) {
if(!is_null($valueMin) && !$this->_isInt($valueMin)) if (!is_null($valueMin)) {
throw new InvalidArgumentException('La valeur n\'est pas un entier ou 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; return $this;
} }
/** /**
@ -125,10 +129,14 @@ class IntegerParser implements IValueParser {
* @return $this * @return $this
*/ */
public function setValueMax($valueMax = null) { public function setValueMax($valueMax = null) {
if(!is_null($valueMax) && !$this->_isInt($valueMax)) if (!is_null($valueMax)) {
throw new InvalidArgumentException('La valeur n\'est pas un entier ou 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; return $this;
} }
/** /**

@ -1,31 +1,31 @@
<?php <?php
/** /**
* Déclaration de l'interface CommandLine\Argument\Option\IArgumentValue * Déclaration de l'interface CommandLine\Argument\Option\IArgumentValue
*/ */
namespace CommandLine\Argument\Value; namespace CommandLine\Argument\Value;
use CommandLine\Argument\IArgument; use CommandLine\Argument\IArgument;
/** /**
* Interface à implémenter si l'argument est de type "valeur" * Interface à implémenter si l'argument est de type "valeur"
* *
* @package CommandLine\Argument\value * @package CommandLine\Argument\value
*/ */
interface IArgumentValue extends IArgument { interface IArgumentValue extends IArgument {
/** /**
* Le nombre minimum d'occurence. * Le nombre minimum d'occurence.
* *
* Généralement 0 (falcultatif) ou 1 (obligatoire). * Généralement 0 (falcultatif) ou 1 (obligatoire).
* *
* @return int Le nombre minimum d'occurence * @return int Le nombre minimum d'occurence
*/ */
public function getOccurMin(); public function getOccurMin();
/** /**
* Le nombre maximum d'occurence. * Le nombre maximum d'occurence.
* *
* Généralement 1 ou Null (illimité). * Généralement 1 ou Null (illimité).
* *
* @return int|null Le nombre maximum d'occurence * @return int|null Le nombre maximum d'occurence
*/ */
public function getOccurMax(); public function getOccurMax();
} }

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

@ -1,12 +1,12 @@
<?php <?php
/** /**
* Déclaration de l'interface d'exception CommandLine\Exception\IException. * Déclaration de l'interface d'exception CommandLine\Exception\IException.
*/ */
namespace CommandLine\Exception; namespace CommandLine\Exception;
/** /**
* Interface pour toutes les exceptions du module. * Interface pour toutes les exceptions du module.
* *
* @package CommandLine\Exception * @package CommandLine\Exception
*/ */
interface IException {} interface IException {}

@ -1,17 +1,16 @@
<?php <?php
/** /**
* Déclaration de l'exception CommandLine\Exception\MissingArgument * Déclaration de l'exception CommandLine\Exception\MissingArgument
*/ */
namespace CommandLine\Exception; namespace CommandLine\Exception;
use CommandLine\Solution; use RuntimeException;
use RuntimeException;
/**
/** * Exception levée quand un argument est manquant lors d'un parsage d'une solution
* Exception levée quand un argument est manquant lors d'un parsage d'une solution *
* * @package CommandLine\Exception
* @package CommandLine\Exception *
* * @see Solution::parseExplicit()
* @see Solution::parseExplicit() */
*/
class MissingArgument extends RuntimeException implements IException {} class MissingArgument extends RuntimeException implements IException {}

@ -1,14 +1,14 @@
<?php <?php
/** /**
* Déclaration de l'exception CommandLine\Exception\TooMushValues * Déclaration de l'exception CommandLine\Exception\TooMushValues
*/ */
namespace CommandLine\Exception; namespace CommandLine\Exception;
use RuntimeException; use RuntimeException;
/** /**
* Exception levée quand il y a des valeurs en trop lors d'un parsage d'une solution * Exception levée quand il y a des valeurs en trop lors d'un parsage d'une solution
* *
* @package CommandLine\Exception * @package CommandLine\Exception
*/ */
class TooMuchValues extends RuntimeException implements IException {} class TooMuchValues extends RuntimeException implements IException {}
Loading…
Cancel
Save