You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
phpcommandline/src/CommandLine/Argument/Option/Value.php

91 lines
2.5 KiB
PHP

<?php
/**
* Déclaration de la classe CommandLine\Argument\Option\Value
*/
namespace jrosset\CommandLine\Argument\Option;
use jrosset\CommandLine\Argument\IArgumentValueDescription;
use jrosset\CommandLine\Argument\Parser\IValueParser;
use jrosset\CommandLine\Argument\ParseResult;
use jrosset\CommandLine\Exception\IncorrectParse;
use RuntimeException;
/**
* Option contenant une valeur
*
* @package CommandLine\Argument\Option
*/
class Value extends OptionAbstract implements IArgumentValueDescription {
/**
* @var IValueParser Parseur pour la valeur
*/
protected IValueParser $valueParser;
/**
* Crée un nouvel argument de type option
*
* @param string $name Le nom
* @param string|null $description La description
* @param IValueParser $valueParser Le parseur de valeur
* @param string|null $tagLong Le tag long
* @param string|null $tagShort Le tag court
*/
public function __construct (string $name, ?string $description, IValueParser $valueParser, ?string $tagLong = null, ?string $tagShort = null) {
parent::__construct($name, $description, $tagLong, $tagShort);
$this->setValueParser($valueParser);
}
/**
* @inheritDoc
*/
public function parse (array $args): ?ParseResult {
try {
if (!$this->_parseTag($args[0])) {
return null;
}
if (count($args) < 2 || is_null($args[1])) {
throw new IncorrectParse('La seconde valeur de l\'argument manquante');
}
return new ParseResult($this->valueParser->parseValue($args[1]), 2);
}
catch (RuntimeException $e) {
throw new IncorrectParse('Échec du parsage de la valeur "' . $args[0] . '"', 0, $e);
}
}
/**
* @inheritDoc
*/
public function getValueDescription (): string {
return $this->valueParser->getValueDescription();
}
/**
* Le parseur de valeur.
*
* @return IValueParser Le parseur.
*
* @see $valueParser
*
* @noinspection PhpUnused
*/
public function getValueParser (): IValueParser {
return $this->valueParser;
}
/**
* Définit le parseur de valeur
*
* @param IValueParser $valueParser Le parseur
*
* @return $this
*
* @see $valueParser
*/
public function setValueParser (IValueParser $valueParser): self {
$this->valueParser = $valueParser;
return $this;
}
}