From b3d82fff86746ba17fa0cfc26e302d49fbae4bf3 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Wed, 14 May 2025 11:41:30 +0200 Subject: [PATCH] Create CommandArgumentList --- .../CommandCall/CommandArgumentList.php | 67 +++++++++++++++++++ src/CliProgram/CommandCall/CommandCall.php | 29 ++++---- 2 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 src/CliProgram/CommandCall/CommandArgumentList.php diff --git a/src/CliProgram/CommandCall/CommandArgumentList.php b/src/CliProgram/CommandCall/CommandArgumentList.php new file mode 100644 index 0000000..59e071c --- /dev/null +++ b/src/CliProgram/CommandCall/CommandArgumentList.php @@ -0,0 +1,67 @@ + The arguments + */ + protected array $arguments; + + /** + * Initialization + * + * @param Traversable|null $arguments The arguments + */ + public function __construct (?Traversable $arguments = null) { + $this->arguments = []; + if ($arguments !== null) { + foreach ($arguments as $name => $value) { + $this->arguments[$name] = $value; + } + } + } + + /** + * The arguments + * + * @return array The arguments + */ + public function getArguments (): array { + return $this->arguments; + } + + /** + * Add an argument to the list + * + * Replace it if existing. + * + * @param string $name The argument name + * @param mixed $value The argument value + * + * @return $this + */ + public function addArgument (string $name, mixed $value): static { + $this->arguments[$name] = $value; + return $this; + } + /** + * Add an option to the list + * + * Replace it if existing. + * + * @param string $name The option name (with or without the leading dashes) + * @param mixed $value The option value + * + * @return $this + */ + public function addOption (string $name, mixed $value): static { + $this->arguments[(str_starts_with($name, '-') ? '' : '--') . $name] = $value; + return $this; + } +} \ No newline at end of file diff --git a/src/CliProgram/CommandCall/CommandCall.php b/src/CliProgram/CommandCall/CommandCall.php index 93f77f2..a6596c3 100644 --- a/src/CliProgram/CommandCall/CommandCall.php +++ b/src/CliProgram/CommandCall/CommandCall.php @@ -20,17 +20,17 @@ class CommandCall { */ private string $commandName; /** - * @var Arrayy The command arguments + * @var CommandArgumentList The command arguments */ - private Arrayy $commandArguments; + private CommandArgumentList $commandArguments; /** - * @param string|Command $commandName The new command name - * @param null|Arrayy $commandArguments The command new arguments + * @param string|Command $commandName The new command name + * @param CommandArgumentList|Arrayy|null $commandArguments The command new arguments */ - public function __construct (string|Command $commandName, ?Arrayy $commandArguments = null) { + public function __construct (string|Command $commandName, CommandArgumentList|Arrayy|null $commandArguments = null) { $this->setCommandName($commandName); - $this->setCommandArguments($commandArguments ?? new Arrayy()); + $this->setCommandArguments($commandArguments ?? new CommandArgumentList()); } /** @@ -40,9 +40,12 @@ class CommandCall { */ public function generateInput (): InputInterface { return new ArrayInput( - (clone $this->getCommandArguments()) - ->prepend($this->getCommandName(), 'command') - ->toArray() + array_merge( + [ + 'command' => $this->getCommandName(), + ], + $this->getCommandArguments()->getArguments() + ) ); } /** @@ -86,18 +89,18 @@ class CommandCall { * * @return Arrayy The command arguments */ - public function getCommandArguments (): Arrayy { + public function getCommandArguments (): CommandArgumentList { return $this->commandArguments; } /** * Set the command arguments * - * @param Arrayy $commandArguments The command new arguments + * @param CommandArgumentList|Arrayy $commandArguments The command new arguments * * @return $this */ - public function setCommandArguments (Arrayy $commandArguments): self { - $this->commandArguments = $commandArguments; + public function setCommandArguments (CommandArgumentList|Arrayy $commandArguments): self { + $this->commandArguments = $commandArguments instanceof CommandArgumentList ? $commandArguments : new CommandArgumentList($commandArguments); return $this; } } \ No newline at end of file