From 0742795afa9df7bb3000679573da91429738881a Mon Sep 17 00:00:00 2001 From: darkelfe14728 Date: Sun, 22 Sep 2019 16:29:55 +0200 Subject: [PATCH] Code reorganization (PSR-4) --- .../CommandLine/Argument/ArgumentAbstract.php | 0 .../CommandLine/Argument/IArgument.php | 0 .../Argument/IArgumentValueDescription.php | 36 +++---- .../CommandLine/Argument/Option/Flag.php | 0 .../Argument/Option/FlagWithReverse.php | 94 +++++++++---------- .../Argument/Option/IArgumentOption.php | 90 +++++++++--------- .../Option/IArgumentOptionSecondary.php | 0 .../Argument/Option/OptionAbstract.php | 17 ++++ .../CommandLine/Argument/Option/Value.php | 0 .../CommandLine/Argument/ParseResult.php | 0 .../Argument/Parser/BooleanParser.php | 2 +- .../Argument/Parser/DecimalParser.php | 20 ++-- .../Argument/Parser/EnumParser.php | 0 .../Argument/Parser/IValueParser.php | 0 .../Argument/Parser/IntegerParser.php | 20 ++-- .../Argument/Parser/RegexParser.php | 0 .../Argument/Parser/StringParser.php | 0 .../CommandLine/Argument/Value/FixedValue.php | 0 .../Argument/Value/IArgumentValue.php | 60 ++++++------ .../CommandLine/Argument/Value/Value.php | 0 .../Argument/Value/ValueAbstract.php | 0 .../CommandLine/CommandLine.php | 11 ++- .../CommandLine/Exception/IException.php | 22 ++--- .../CommandLine/Exception/IncorrectParse.php | 0 .../CommandLine/Exception/MissingArgument.php | 31 +++--- .../CommandLine/Exception/TooMuchValues.php | 26 ++--- 26 files changed, 232 insertions(+), 197 deletions(-) rename Argument/ArgumentAbstract.class.php => src/CommandLine/Argument/ArgumentAbstract.php (100%) rename Argument/IArgument.class.php => src/CommandLine/Argument/IArgument.php (100%) rename Argument/IArgumentValueDescription.class.php => src/CommandLine/Argument/IArgumentValueDescription.php (95%) rename Argument/Option/Flag.class.php => src/CommandLine/Argument/Option/Flag.php (100%) rename Argument/Option/FlagWithReverse.class.php => src/CommandLine/Argument/Option/FlagWithReverse.php (97%) rename Argument/Option/IArgumentOption.class.php => src/CommandLine/Argument/Option/IArgumentOption.php (95%) rename Argument/Option/IArgumentOptionSecondary.class.php => src/CommandLine/Argument/Option/IArgumentOptionSecondary.php (100%) rename Argument/Option/OptionAbstract.class.php => src/CommandLine/Argument/Option/OptionAbstract.php (90%) rename Argument/Option/Value.class.php => src/CommandLine/Argument/Option/Value.php (100%) rename Argument/ParseResult.class.php => src/CommandLine/Argument/ParseResult.php (100%) rename Argument/Parser/BooleanParser.class.php => src/CommandLine/Argument/Parser/BooleanParser.php (96%) rename Argument/Parser/DecimalParser.class.php => src/CommandLine/Argument/Parser/DecimalParser.php (84%) rename Argument/Parser/EnumParser.class.php => src/CommandLine/Argument/Parser/EnumParser.php (100%) rename Argument/Parser/IValueParser.class.php => src/CommandLine/Argument/Parser/IValueParser.php (100%) rename Argument/Parser/IntegerParser.class.php => src/CommandLine/Argument/Parser/IntegerParser.php (85%) rename Argument/Parser/RegexParser.class.php => src/CommandLine/Argument/Parser/RegexParser.php (100%) rename Argument/Parser/StringParser.class.php => src/CommandLine/Argument/Parser/StringParser.php (100%) rename Argument/Value/FixedValue.class.php => src/CommandLine/Argument/Value/FixedValue.php (100%) rename Argument/Value/IArgumentValue.class.php => src/CommandLine/Argument/Value/IArgumentValue.php (95%) rename Argument/Value/Value.class.php => src/CommandLine/Argument/Value/Value.php (100%) rename Argument/Value/ValueAbstract.class.php => src/CommandLine/Argument/Value/ValueAbstract.php (100%) rename CommandLine.class.php => src/CommandLine/CommandLine.php (98%) rename Exception/IException.class.php => src/CommandLine/Exception/IException.php (86%) rename Exception/IncorrectParse.class.php => src/CommandLine/Exception/IncorrectParse.php (100%) rename Exception/MissingArgument.class.php => src/CommandLine/Exception/MissingArgument.php (88%) rename Exception/TooMuchValues.class.php => src/CommandLine/Exception/TooMuchValues.php (94%) diff --git a/Argument/ArgumentAbstract.class.php b/src/CommandLine/Argument/ArgumentAbstract.php similarity index 100% rename from Argument/ArgumentAbstract.class.php rename to src/CommandLine/Argument/ArgumentAbstract.php diff --git a/Argument/IArgument.class.php b/src/CommandLine/Argument/IArgument.php similarity index 100% rename from Argument/IArgument.class.php rename to src/CommandLine/Argument/IArgument.php diff --git a/Argument/IArgumentValueDescription.class.php b/src/CommandLine/Argument/IArgumentValueDescription.php similarity index 95% rename from Argument/IArgumentValueDescription.class.php rename to src/CommandLine/Argument/IArgumentValueDescription.php index 468d53c..bfe303b 100644 --- a/Argument/IArgumentValueDescription.class.php +++ b/src/CommandLine/Argument/IArgumentValueDescription.php @@ -1,19 +1,19 @@ -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); - } +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); + } } \ No newline at end of file diff --git a/Argument/Option/IArgumentOption.class.php b/src/CommandLine/Argument/Option/IArgumentOption.php similarity index 95% rename from Argument/Option/IArgumentOption.class.php rename to src/CommandLine/Argument/Option/IArgumentOption.php index 9d67878..6ca838c 100644 --- a/Argument/Option/IArgumentOption.class.php +++ b/src/CommandLine/Argument/Option/IArgumentOption.php @@ -1,46 +1,46 @@ -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; } diff --git a/Argument/Option/Value.class.php b/src/CommandLine/Argument/Option/Value.php similarity index 100% rename from Argument/Option/Value.class.php rename to src/CommandLine/Argument/Option/Value.php diff --git a/Argument/ParseResult.class.php b/src/CommandLine/Argument/ParseResult.php similarity index 100% rename from Argument/ParseResult.class.php rename to src/CommandLine/Argument/ParseResult.php diff --git a/Argument/Parser/BooleanParser.class.php b/src/CommandLine/Argument/Parser/BooleanParser.php similarity index 96% rename from Argument/Parser/BooleanParser.class.php rename to src/CommandLine/Argument/Parser/BooleanParser.php index fdbb477..bd0990e 100644 --- a/Argument/Parser/BooleanParser.class.php +++ b/src/CommandLine/Argument/Parser/BooleanParser.php @@ -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 diff --git a/Argument/Parser/DecimalParser.class.php b/src/CommandLine/Argument/Parser/DecimalParser.php similarity index 84% rename from Argument/Parser/DecimalParser.class.php rename to src/CommandLine/Argument/Parser/DecimalParser.php index 0297137..2aeacdf 100644 --- a/Argument/Parser/DecimalParser.class.php +++ b/src/CommandLine/Argument/Parser/DecimalParser.php @@ -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; } /** diff --git a/Argument/Parser/EnumParser.class.php b/src/CommandLine/Argument/Parser/EnumParser.php similarity index 100% rename from Argument/Parser/EnumParser.class.php rename to src/CommandLine/Argument/Parser/EnumParser.php diff --git a/Argument/Parser/IValueParser.class.php b/src/CommandLine/Argument/Parser/IValueParser.php similarity index 100% rename from Argument/Parser/IValueParser.class.php rename to src/CommandLine/Argument/Parser/IValueParser.php diff --git a/Argument/Parser/IntegerParser.class.php b/src/CommandLine/Argument/Parser/IntegerParser.php similarity index 85% rename from Argument/Parser/IntegerParser.class.php rename to src/CommandLine/Argument/Parser/IntegerParser.php index 9633aa1..6ee5406 100644 --- a/Argument/Parser/IntegerParser.class.php +++ b/src/CommandLine/Argument/Parser/IntegerParser.php @@ -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; } /** diff --git a/Argument/Parser/RegexParser.class.php b/src/CommandLine/Argument/Parser/RegexParser.php similarity index 100% rename from Argument/Parser/RegexParser.class.php rename to src/CommandLine/Argument/Parser/RegexParser.php diff --git a/Argument/Parser/StringParser.class.php b/src/CommandLine/Argument/Parser/StringParser.php similarity index 100% rename from Argument/Parser/StringParser.class.php rename to src/CommandLine/Argument/Parser/StringParser.php diff --git a/Argument/Value/FixedValue.class.php b/src/CommandLine/Argument/Value/FixedValue.php similarity index 100% rename from Argument/Value/FixedValue.class.php rename to src/CommandLine/Argument/Value/FixedValue.php diff --git a/Argument/Value/IArgumentValue.class.php b/src/CommandLine/Argument/Value/IArgumentValue.php similarity index 95% rename from Argument/Value/IArgumentValue.class.php rename to src/CommandLine/Argument/Value/IArgumentValue.php index 9ed437e..7ff71f6 100644 --- a/Argument/Value/IArgumentValue.class.php +++ b/src/CommandLine/Argument/Value/IArgumentValue.php @@ -1,31 +1,31 @@ -'; + $type = /** @lang text */''; } 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 diff --git a/Exception/IException.class.php b/src/CommandLine/Exception/IException.php similarity index 86% rename from Exception/IException.class.php rename to src/CommandLine/Exception/IException.php index b19c2c3..c5bf6f2 100644 --- a/Exception/IException.class.php +++ b/src/CommandLine/Exception/IException.php @@ -1,12 +1,12 @@ -