diff --git a/src/CliProgram/CommandCall/CommandArgumentList.php b/src/CliProgram/CommandCall/CommandArgumentList.php index 5b7e190..186a15a 100644 --- a/src/CliProgram/CommandCall/CommandArgumentList.php +++ b/src/CliProgram/CommandCall/CommandArgumentList.php @@ -8,6 +8,19 @@ use Symfony\Component\Console\Input\InputInterface; * A list of arguments for a command call */ class CommandArgumentList { + /** + * Les nom des options 'booléennes' de Symfony + */ + public const SYMFONY_OPTIONS_BOOL = ['help', 'quiet', 'version', 'no-interaction']; + /** + * Les noms des options 'texte' de Symfony + */ + public const SYMFONY_OPTIONS_TEXT = ['ansi']; + /** + * Le nom de l'option de verbosité de Symfony + */ + public const SYMFONY_OPTION_VERBOSITY = 'verbose'; + /** * @var array The arguments */ @@ -28,58 +41,116 @@ class CommandArgumentList { } /** - * Create a list of arguments from {@see InputInterface} + * Copie les arguments d'une {@see InputInterface entrée de commande} * - * @param InputInterface $input The input interface - * @param bool $keepCommandName Keep the command name in the list of arguments? + * @param InputInterface $input L'entrée de commande + * @param bool $keepCommandName Conserver le nom de la commande ? * - * @return static The list of arguments + * @return $this */ - public static function fromInputInterface (InputInterface $input, bool $keepCommandName = false): static { - $args = new static(); - //region Recopie arguments + public function copyArgumentsOf (InputInterface $input, bool $keepCommandName = false): static { foreach ($input->getArguments() as $argumentName => $argumentValue) { - //region Recopie des arguments par défaut de Symfony if ($argumentName == 'command' && !$keepCommandName) { continue; } - //endregion - $args->addArgument($argumentName, $argumentValue); + $this->addArgument($argumentName, $argumentValue); } - //endregion - //region Recopie options + return $this; + } + + /** + * Copie les options d'une {@see InputInterface entrée de commande} + * + * @param InputInterface $input L'entrée de commande + * + * @return $this + */ + public function copyOptionsOf (InputInterface $input): static { + $this->copySymfonyOptionsOf($input); foreach ($input->getOptions() as $optionName => $optionValue) { - //region Recopie des options par défaut de Symfony - if (in_array($optionName, ['help', 'quiet', 'version', 'no-interaction']) && $optionValue === false) { + if (in_array( + $optionName, + array_merge( + self::SYMFONY_OPTIONS_BOOL, + self::SYMFONY_OPTIONS_TEXT, + [self::SYMFONY_OPTION_VERBOSITY] + ) + )) { + // Déjà gérées via $this->copySymfonyOptionsOf + continue; + } + $this->addOption($optionName, $optionValue); + } + return $this; + } + + /** + * Copie les options standard de Symfony d'une {@see InputInterface entrée de commande} + * + * @param InputInterface $input L'entrée de commande + * + * @return $this + */ + public function copySymfonyOptionsOf (InputInterface $input): static { + $this->copyVerbosityOptionOf($input); + foreach ($input->getOptions() as $optionName => $optionValue) { + if (in_array($optionName, self::SYMFONY_OPTIONS_BOOL) && $optionValue === false) { // Symfony se contente de regarder si le nom de l'option est donné, peu importe sa valeur, donc ils ne sont pas recopiés si à faux continue; } - if ($optionName == 'ansi' && $optionValue === null) { + elseif (in_array($optionName, self::SYMFONY_OPTIONS_TEXT) && $optionValue === null) { // Symfony se contente de regarder si le nom de l'option est donné, peu importe sa valeur, donc n'est pas recopié si aucune valeur continue; } - if ($optionName == 'verbose') { - // Pour la verbosité, $input->getOptions() se contente de signaler "verbose" = "vrai" sans tenir compte du niveau de verbosité - // Donc on doit tester tous les cas un par un (et ne rien recopier si pas de verbosité) - // Teste du plus verbeux au moins verbeux car "-vv" matche si "-vvv" transmis - if ($input->hasParameterOption('-vvv')) { - $args->addOption('-vvv'); - } - elseif ($input->hasParameterOption('-vv')) { - $args->addOption('-vv'); - } - elseif ($input->hasParameterOption('-v')) { - $args->addOption('-v'); - } - elseif ($optionValue === true) { - $args->addOption('verbose'); - } + elseif ($optionName == self::SYMFONY_OPTION_VERBOSITY) { + // Déjà géré via $this->copyVerbosityOptionOf continue; } - //endregion - $args->addOption($optionName, $optionValue); + $this->addOption($optionName, $optionValue); } - //endregion + return $this; + } + /** + * Copie l'option de verbosité d'une {@see InputInterface entrée de commande} + * + * @param InputInterface $input L'entrée de commande + * + * @return $this + */ + public function copyVerbosityOptionOf (InputInterface $input): static { + $options = $input->getOptions(); + if (array_key_exists(self::SYMFONY_OPTION_VERBOSITY, $options)) { + // Pour la verbosité, $input->getOptions() se contente de signaler "verbose" = "vrai" sans tenir compte du niveau de verbosité + // Donc on doit tester tous les cas un par un (et ne rien recopier si pas de verbosité) + // Teste du plus verbeux au moins verbeux car "-vv" matche si "-vvv" transmis + if ($input->hasParameterOption('-vvv')) { + $this->addOption('-vvv'); + } + elseif ($input->hasParameterOption('-vv')) { + $this->addOption('-vv'); + } + elseif ($input->hasParameterOption('-v')) { + $this->addOption('-v'); + } + elseif ($options[self::SYMFONY_OPTION_VERBOSITY] === true) { + $this->addOption(self::SYMFONY_OPTION_VERBOSITY); + } + } + return $this; + } + + /** + * Create a list of arguments from {@see InputInterface} + * + * @param InputInterface $input The input interface + * @param bool $keepCommandName Keep the command name in the list of arguments? + * + * @return static The list of arguments + */ + public static function fromInputInterface (InputInterface $input, bool $keepCommandName = false): static { + $args = new static(); + $args->copyArgumentsOf($input, $keepCommandName); + $args->copyOptionsOf($input); return $args; }