CliOption.php 2.6 KB

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