CliOption.php 2.7 KB

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