CliOption.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. final class CliOption {
  4. public const VALUE_NONE = 'none';
  5. public const VALUE_REQUIRED = 'required';
  6. public const VALUE_OPTIONAL = 'optional';
  7. private string $valueTaken = self::VALUE_REQUIRED;
  8. /** @var array{type:string,isArray:bool} $types */
  9. private array $types = ['type' => 'string', 'isArray' => false];
  10. private string $optionalValueDefault = '';
  11. private ?string $deprecatedAlias = null;
  12. public function __construct(private readonly string $longAlias, private readonly ?string $shortAlias = null) {
  13. }
  14. /** Sets this option to be treated as a flag. */
  15. public function withValueNone(): self {
  16. $this->valueTaken = static::VALUE_NONE;
  17. return $this;
  18. }
  19. /** Sets this option to always require a value when used. */
  20. public function withValueRequired(): self {
  21. $this->valueTaken = static::VALUE_REQUIRED;
  22. return $this;
  23. }
  24. /**
  25. * Sets this option to accept both values and flag behavior.
  26. * @param string $optionalValueDefault When this option is used as a flag it receives this value as input.
  27. */
  28. public function withValueOptional(string $optionalValueDefault = ''): self {
  29. $this->valueTaken = static::VALUE_OPTIONAL;
  30. $this->optionalValueDefault = $optionalValueDefault;
  31. return $this;
  32. }
  33. public function typeOfString(): self {
  34. $this->types = ['type' => 'string', 'isArray' => false];
  35. return $this;
  36. }
  37. public function typeOfInt(): self {
  38. $this->types = ['type' => 'int', 'isArray' => false];
  39. return $this;
  40. }
  41. public function typeOfBool(): self {
  42. $this->types = ['type' => 'bool', 'isArray' => false];
  43. return $this;
  44. }
  45. public function typeOfArrayOfString(): self {
  46. $this->types = ['type' => 'string', 'isArray' => true];
  47. return $this;
  48. }
  49. public function deprecatedAs(string $deprecated): self {
  50. $this->deprecatedAlias = $deprecated;
  51. return $this;
  52. }
  53. public function getValueTaken(): string {
  54. return $this->valueTaken;
  55. }
  56. public function getOptionalValueDefault(): string {
  57. return $this->optionalValueDefault;
  58. }
  59. public function getDeprecatedAlias(): ?string {
  60. return $this->deprecatedAlias;
  61. }
  62. public function getLongAlias(): string {
  63. return $this->longAlias;
  64. }
  65. public function getShortAlias(): ?string {
  66. return $this->shortAlias;
  67. }
  68. /** @return array{type:string,isArray:bool} */
  69. public function getTypes(): array {
  70. return $this->types;
  71. }
  72. /** @return string[] */
  73. public function getAliases(): array {
  74. $aliases = [
  75. $this->longAlias,
  76. $this->shortAlias,
  77. $this->deprecatedAlias,
  78. ];
  79. return array_filter($aliases);
  80. }
  81. }