Create CommandArgumentList
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue