| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- declare(strict_types=1);
- final class CliOption {
- public const VALUE_NONE = 'none';
- public const VALUE_REQUIRED = 'required';
- public const VALUE_OPTIONAL = 'optional';
- private string $longAlias;
- private ?string $shortAlias;
- private string $valueTaken = self::VALUE_REQUIRED;
- /** @var array{type:string,isArray:bool} $types */
- private array $types = ['type' => 'string', 'isArray' => false];
- private string $optionalValueDefault = '';
- private ?string $deprecatedAlias = null;
- public function __construct(string $longAlias, ?string $shortAlias = null) {
- $this->longAlias = $longAlias;
- $this->shortAlias = $shortAlias;
- }
- /** Sets this option to be treated as a flag. */
- public function withValueNone(): self {
- $this->valueTaken = static::VALUE_NONE;
- return $this;
- }
- /** Sets this option to always require a value when used. */
- public function withValueRequired(): self {
- $this->valueTaken = static::VALUE_REQUIRED;
- return $this;
- }
- /**
- * Sets this option to accept both values and flag behavior.
- * @param string $optionalValueDefault When this option is used as a flag it receives this value as input.
- */
- public function withValueOptional(string $optionalValueDefault = ''): self {
- $this->valueTaken = static::VALUE_OPTIONAL;
- $this->optionalValueDefault = $optionalValueDefault;
- return $this;
- }
- public function typeOfString(): self {
- $this->types = ['type' => 'string', 'isArray' => false];
- return $this;
- }
- public function typeOfInt(): self {
- $this->types = ['type' => 'int', 'isArray' => false];
- return $this;
- }
- public function typeOfBool(): self {
- $this->types = ['type' => 'bool', 'isArray' => false];
- return $this;
- }
- public function typeOfArrayOfString(): self {
- $this->types = ['type' => 'string', 'isArray' => true];
- return $this;
- }
- public function deprecatedAs(string $deprecated): self {
- $this->deprecatedAlias = $deprecated;
- return $this;
- }
- public function getValueTaken(): string {
- return $this->valueTaken;
- }
- public function getOptionalValueDefault(): string {
- return $this->optionalValueDefault;
- }
- public function getDeprecatedAlias(): ?string {
- return $this->deprecatedAlias;
- }
- public function getLongAlias(): string {
- return $this->longAlias;
- }
- public function getShortAlias(): ?string {
- return $this->shortAlias;
- }
- /** @return array{type:string,isArray:bool} */
- public function getTypes(): array {
- return $this->types;
- }
- /** @return string[] */
- public function getAliases(): array {
- $aliases = [
- $this->longAlias,
- $this->shortAlias,
- $this->deprecatedAlias,
- ];
- return array_filter($aliases);
- }
- }
|