Create CommandArgumentList

master
Julien Rosset 2 months ago
parent 8152f9a673
commit b3d82fff86

@ -0,0 +1,67 @@
<?php
namespace jrosset\CliProgram\CommandCall;
use Traversable;
/**
* A list of arguments for a command call
*/
class CommandArgumentList {
/**
* @var array<string, mixed> 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<string, mixed> 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;
}
}

@ -20,17 +20,17 @@ class CommandCall {
*/ */
private string $commandName; private string $commandName;
/** /**
* @var Arrayy<string, mixed> The command arguments * @var CommandArgumentList The command arguments
*/ */
private Arrayy $commandArguments; private CommandArgumentList $commandArguments;
/** /**
* @param string|Command $commandName The new command name * @param string|Command $commandName The new command name
* @param null|Arrayy<string, mixed> $commandArguments The command new arguments * @param CommandArgumentList|Arrayy<string, mixed>|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->setCommandName($commandName);
$this->setCommandArguments($commandArguments ?? new Arrayy()); $this->setCommandArguments($commandArguments ?? new CommandArgumentList());
} }
/** /**
@ -40,9 +40,12 @@ class CommandCall {
*/ */
public function generateInput (): InputInterface { public function generateInput (): InputInterface {
return new ArrayInput( return new ArrayInput(
(clone $this->getCommandArguments()) array_merge(
->prepend($this->getCommandName(), 'command') [
->toArray() 'command' => $this->getCommandName(),
],
$this->getCommandArguments()->getArguments()
)
); );
} }
/** /**
@ -86,18 +89,18 @@ class CommandCall {
* *
* @return Arrayy<string, mixed> The command arguments * @return Arrayy<string, mixed> The command arguments
*/ */
public function getCommandArguments (): Arrayy { public function getCommandArguments (): CommandArgumentList {
return $this->commandArguments; return $this->commandArguments;
} }
/** /**
* Set the command arguments * Set the command arguments
* *
* @param Arrayy<string, mixed> $commandArguments The command new arguments * @param CommandArgumentList|Arrayy $commandArguments The command new arguments
* *
* @return $this * @return $this
*/ */
public function setCommandArguments (Arrayy $commandArguments): self { public function setCommandArguments (CommandArgumentList|Arrayy $commandArguments): self {
$this->commandArguments = $commandArguments; $this->commandArguments = $commandArguments instanceof CommandArgumentList ? $commandArguments : new CommandArgumentList($commandArguments);
return $this; return $this;
} }
} }
Loading…
Cancel
Save