Code reorganization (PSR-4)

master
Julien Rosset 6 years ago
parent f950043b9c
commit 0742795afa

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

@ -1,48 +1,48 @@
<?php
/**
* Déclare la classe CommandLine\Argument\Option\OptionFlagWithReverse.
*/
namespace 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 () {
$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);
}
<?php
/**
* Déclare la classe CommandLine\Argument\Option\OptionFlagWithReverse.
*/
namespace 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 () {
$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 +1,46 @@
<?php
/**
* Déclaration de l'interface CommandLine\Argument\Option\IArgumentOption
*/
namespace CommandLine\Argument\Option;
use 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();
/**
* Le tag long.
*
* @return string Le tag long.
*
* @see $_tagLong
*/
public function getTagLong();
/**
* Est-ce que l'argument est autorisé plusieurs fois ?
*
* @return boolean True si l'argument est autorisé plusieurs fois, sinon False
*/
public function allowMultiple();
/**
* 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();
<?php
/**
* Déclaration de l'interface CommandLine\Argument\Option\IArgumentOption
*/
namespace CommandLine\Argument\Option;
use 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();
/**
* Le tag long.
*
* @return string Le tag long.
*
* @see $_tagLong
*/
public function getTagLong();
/**
* Est-ce que l'argument est autorisé plusieurs fois ?
*
* @return boolean True si l'argument est autorisé plusieurs fois, sinon False
*/
public function allowMultiple();
/**
* 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();
}

@ -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;
}
/**

@ -1,31 +1,31 @@
<?php
/**
* Déclaration de l'interface CommandLine\Argument\Option\IArgumentValue
*/
namespace CommandLine\Argument\Value;
use 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();
/**
* Le nombre maximum d'occurence.
*
* Généralement 1 ou Null (illimité).
*
* @return int|null Le nombre maximum d'occurence
*/
public function getOccurMax();
<?php
/**
* Déclaration de l'interface CommandLine\Argument\Option\IArgumentValue
*/
namespace CommandLine\Argument\Value;
use 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();
/**
* Le nombre maximum d'occurence.
*
* Généralement 1 ou Null (illimité).
*
* @return int|null Le nombre maximum d'occurence
*/
public function getOccurMax();
}

@ -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

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

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

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