Add CommandArgumentList::fromInputInterface

master 3.11.0
Julien Rosset 3 months ago
parent 1d4075240e
commit 19f11dd899

@ -2,7 +2,7 @@
namespace jrosset\CliProgram\CommandCall;
use Traversable;
use Symfony\Component\Console\Input\InputInterface;
/**
* A list of arguments for a command call
@ -16,9 +16,9 @@ class CommandArgumentList {
/**
* Initialization
*
* @param Traversable|null $arguments The arguments
* @param iterable|null $arguments The arguments
*/
public function __construct (?Traversable $arguments = null) {
public function __construct (?iterable $arguments = null) {
$this->arguments = [];
if ($arguments !== null) {
foreach ($arguments as $name => $value) {
@ -27,6 +27,62 @@ class CommandArgumentList {
}
}
/**
* 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();
//region Recopie arguments
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);
}
//endregion
//region Recopie options
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) {
// 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) {
// 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');
}
continue;
}
//endregion
$args->addOption($optionName, $optionValue);
}
//endregion
return $args;
}
/**
* The arguments
*

Loading…
Cancel
Save