CommandArgumentList : ajout méthode copie arguments/options

master 3.18.0
Julien Rosset 3 weeks ago
parent eae1767640
commit 77831fc902

@ -8,6 +8,19 @@ use Symfony\Component\Console\Input\InputInterface;
* A list of arguments for a command call * A list of arguments for a command call
*/ */
class CommandArgumentList { 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<string, mixed> The arguments * @var array<string, mixed> The arguments
*/ */
@ -28,58 +41,116 @@ class CommandArgumentList {
} }
/** /**
* Create a list of arguments from {@see InputInterface} * Copie les <i>arguments</i> d'une {@see InputInterface entrée de commande}
* *
* @param InputInterface $input The input interface * @param InputInterface $input L'entrée de commande
* @param bool $keepCommandName Keep the command name in the list of arguments? * @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 { public function copyArgumentsOf (InputInterface $input, bool $keepCommandName = false): static {
$args = new static();
//region Recopie arguments
foreach ($input->getArguments() as $argumentName => $argumentValue) { foreach ($input->getArguments() as $argumentName => $argumentValue) {
//region Recopie des arguments par défaut de Symfony
if ($argumentName == 'command' && !$keepCommandName) { if ($argumentName == 'command' && !$keepCommandName) {
continue; continue;
} }
//endregion $this->addArgument($argumentName, $argumentValue);
$args->addArgument($argumentName, $argumentValue);
} }
//endregion return $this;
//region Recopie options }
/**
* Copie les <i>options</i> 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) { foreach ($input->getOptions() as $optionName => $optionValue) {
//region Recopie des options par défaut de Symfony if (in_array(
if (in_array($optionName, ['help', 'quiet', 'version', 'no-interaction']) && $optionValue === false) { $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 <i>options</i> 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 // 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; 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 // 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; continue;
} }
if ($optionName == 'verbose') { elseif ($optionName == self::SYMFONY_OPTION_VERBOSITY) {
// Pour la verbosité, $input->getOptions() se contente de signaler "verbose" = "vrai" sans tenir compte du niveau de verbosité // Déjà géré via $this->copyVerbosityOptionOf
// 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');
}
continue; continue;
} }
//endregion $this->addOption($optionName, $optionValue);
$args->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; return $args;
} }

Loading…
Cancel
Save