From dcb5b5c410786ada026d17511297bd850dd43c49 Mon Sep 17 00:00:00 2001 From: darkelfe14728 Date: Sat, 14 Dec 2019 19:30:59 +0100 Subject: [PATCH] Add a path parser --- README.md | 4 +- Tests/test.php | 9 +- .../Argument/Parser/PathParser.php | 104 ++++++++++++++++++ .../Argument/Parser/stdStringClass.php | 37 +++++++ 4 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 src/CommandLine/Argument/Parser/PathParser.php create mode 100644 src/CommandLine/Argument/Parser/stdStringClass.php diff --git a/README.md b/README.md index 5dc9cde..017ede2 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ # PHPCommandLine __Command line management library in PHP (CLI)__ -![last release](https://badgen.net/github/release/darkelfe14728/PhpCommandLine) -![nb releases](https://badgen.net/github/releases/darkelfe14728/PhpCommandLine) +![nb releases](https://badgen.net/github/tags/darkelfe14728/PhpCommandLine?label=Nb%20releases) +![last release](https://badgen.net/github/tag/darkelfe14728/PhpCommandLine?label=Last%20release&color=yellow) ![licence](https://badgen.net/badge/license/CC%20BY%204.0/red) A command-line options parser with configurable expects and help auto generation. diff --git a/Tests/test.php b/Tests/test.php index 4f821a8..28df663 100644 --- a/Tests/test.php +++ b/Tests/test.php @@ -5,7 +5,7 @@ require_once '../vendor/autoload.php'; use CommandLine\Argument\Option\Flag; use CommandLine\Argument\Option\Value; use CommandLine\Argument\Parser\IntegerParser; -use CommandLine\Argument\Parser\StringParser; +use CommandLine\Argument\Parser\PathParser; use CommandLine\CommandLine; $cmdline = new CommandLine('Test', 'Programme de test', 'php test.php'); @@ -13,10 +13,11 @@ $cmdline->addDefaultArguments(); $cmdline->addOption(new Flag('array_form', false, 'Sous la forme d\'un tableau ?'."\n".'Ou pas')); $cmdline->addOption((new Value('days', 'Nombre jour', new IntegerParser(0, 365)))->setDefault(3)); $cmdline->addOption((new Value('years', 'Nombre d\'années', new IntegerParser(0)))->setDefault(5)); -$cmdline->addValue((new \CommandLine\Argument\Value\Value('path', 'Chemin sauvegarde images', new StringParser(), true))); +$cmdline->addValue((new \CommandLine\Argument\Value\Value('path', 'Chemin sauvegarde images', new PathParser(), true))); -$args = $cmdline->parseExplicit(array('--help')); +$args = $cmdline->parse(); $cmdline->treatDefaultArguments($args, false); echo "\n"; -var_dump($args); \ No newline at end of file +var_dump($args); +var_dump((string)$args->path); \ No newline at end of file diff --git a/src/CommandLine/Argument/Parser/PathParser.php b/src/CommandLine/Argument/Parser/PathParser.php new file mode 100644 index 0000000..89352e2 --- /dev/null +++ b/src/CommandLine/Argument/Parser/PathParser.php @@ -0,0 +1,104 @@ + /** @lang PhpRegExp */ '@^(?/*(?:[^/\n]+(?:/+[^/\n]+)*/+)?)(?(?[^/\n]+?)(?:(?:\.(?[a-zA-Z0-9_]+))|(?:/+))?)$@', + 'windows' => /** @lang PhpRegExp */ '@^(?(?:(?:(?:(?:[a-zA-Z]:)|[\\\\/])[\\\\/])|(?![\\\\/]))(?:[^<>:"/\\\\|?*]+(?:[\\\\/][^<>:"/\\\\|?*]+)*[\\\\/]+)?)(?(?[^<>:"/\\\\|?*]+?)(?:(?:\.(?[a-zA-Z0-9_]+))|(?:[\\\\/]))?)$@', + ); + + /** + * @var bool Est-ce que le fichier doit exister ? + */ + protected $_mustExist; + + /** + * Crée un nouveau parseur + * + * @param bool $mustExists Est-ce que le chemin doit exister ? + */ + public function __construct ($mustExists = false) { + $this->setMustExists($mustExists); + } + /** + * Spécifie si le chemin doit exister ou non + * + * @param bool $mustExists True si le chemin doit exister, sinon False + * + * @return $this + * + * @see $_mustExists + */ + public function setMustExists ($mustExists = false) { + if (!is_bool($mustExists)) { + throw new InvalidArgumentException('Le contrôle si le chemin doit exister ou non n\'est pas booléen : ' . $mustExists); + } + + $this->_mustExist = $mustExists; + return $this; + } + + /** + * @inheritDoc + */ + public function parseValue ($arg) { + foreach (self::REGEX_PATH as $regex) { + if (preg_match($regex, $arg, $matches, PREG_UNMATCHED_AS_NULL)) { + if ($this->_mustExist && !file_exists($matches[0])) { + throw new UnexpectedValueException('Le chemin n\'existe pas : ' . $arg); + } + + $out = new stdStringClass('fullpath'); + $out->fullpath = $matches[0]; + $out->directory = $matches['directory'] ?? ''; + $out->filename = $matches['filename'] ?? ''; + $out->basename = $matches['basename'] ?? ''; + $out->extension = $matches['extension'] ?? ''; + + return $out; + } + } + + throw new UnexpectedValueException('La valeur n\'est pas un chemin valide : ' . $arg); + } + + /** + * @inheritDoc + */ + public function getValueDescription () { + return 'path'; + } + + /** + * Est-ce que le chemin doit exister ? + * + * @return bool True si le chemin doit exister, sinon False + * + * @see $_mustExists + */ + public function getMustExist () { + return $this->_mustExist; + } +} \ No newline at end of file diff --git a/src/CommandLine/Argument/Parser/stdStringClass.php b/src/CommandLine/Argument/Parser/stdStringClass.php new file mode 100644 index 0000000..ce2511d --- /dev/null +++ b/src/CommandLine/Argument/Parser/stdStringClass.php @@ -0,0 +1,37 @@ +_property = $property; + } + + /** + * Converti l'instance en chaine de caractère + * + * @return string La valeur de l'attribut choisi (convertion forcé en chaine de caractères) + */ + public function __toString () { + return (string)$this->{$this->_property}; + } +} \ No newline at end of file