cli-barcode PHP
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

163 líneas
4.3 KiB

  1. <?php
  2. namespace Ulrichsg\Getopt;
  3. /**
  4. * Represents an option that Getopt accepts.
  5. */
  6. class Option
  7. {
  8. private $short;
  9. private $long;
  10. private $mode;
  11. private $description = '';
  12. private $argument;
  13. /**
  14. * Creates a new option.
  15. *
  16. * @param string $short the option's short name (a single letter or digit) or null for long-only options
  17. * @param string $long the option's long name (a string of 2+ letter/digit/_/- characters, starting with a letter
  18. * or digit) or null for short-only options
  19. * @param int $mode whether the option can/must have an argument (one of the constants defined in the Getopt class)
  20. * (optional, defaults to no argument)
  21. * @throws \InvalidArgumentException if both short and long name are null
  22. */
  23. public function __construct($short, $long, $mode = Getopt::NO_ARGUMENT)
  24. {
  25. if (!$short && !$long) {
  26. throw new \InvalidArgumentException("The short and long name may not both be empty");
  27. }
  28. $this->setShort($short);
  29. $this->setLong($long);
  30. $this->setMode($mode);
  31. $this->argument = new Argument();
  32. }
  33. /**
  34. * Defines a description for the option. This is only used for generating usage information.
  35. *
  36. * @param string $description
  37. * @return Option this object (for chaining calls)
  38. */
  39. public function setDescription($description)
  40. {
  41. $this->description = $description;
  42. return $this;
  43. }
  44. /**
  45. * Defines a default value for the option.
  46. *
  47. * @param mixed $value
  48. * @return Option this object (for chaining calls)
  49. */
  50. public function setDefaultValue($value)
  51. {
  52. $this->argument->setDefaultValue($value);
  53. return $this;
  54. }
  55. /**
  56. * Defines a validation function for the option.
  57. *
  58. * @param callable $function
  59. * @return Option this object (for chaining calls)
  60. */
  61. public function setValidation($function)
  62. {
  63. $this->argument->setValidation($function);
  64. return $this;
  65. }
  66. /**
  67. * Sets the argument object directly.
  68. *
  69. * @param Argument $arg
  70. * @return Option this object (for chaining calls)
  71. */
  72. public function setArgument(Argument $arg)
  73. {
  74. if ($this->mode == Getopt::NO_ARGUMENT) {
  75. throw new \InvalidArgumentException("Option should not have any argument");
  76. }
  77. $this->argument = $arg;
  78. return $this;
  79. }
  80. /**
  81. * Returns true if the given string is equal to either the short or the long name.
  82. *
  83. * @param string $string
  84. * @return bool
  85. */
  86. public function matches($string)
  87. {
  88. return ($string === $this->short) || ($string === $this->long);
  89. }
  90. public function short()
  91. {
  92. return $this->short;
  93. }
  94. public function long()
  95. {
  96. return $this->long;
  97. }
  98. public function mode()
  99. {
  100. return $this->mode;
  101. }
  102. public function getDescription()
  103. {
  104. return $this->description;
  105. }
  106. /**
  107. * Retrieve the argument object
  108. *
  109. * @return Argument
  110. */
  111. public function getArgument()
  112. {
  113. return $this->argument;
  114. }
  115. /**
  116. * Fluent interface for constructor so options can be added during construction
  117. * @see Options::__construct()
  118. */
  119. public static function create($short, $long, $mode = Getopt::NO_ARGUMENT)
  120. {
  121. return new self($short, $long, $mode);
  122. }
  123. private function setShort($short)
  124. {
  125. if (!(is_null($short) || preg_match("/^[a-zA-Z0-9]$/", $short))) {
  126. throw new \InvalidArgumentException("Short option must be null or a letter/digit, found '$short'");
  127. }
  128. $this->short = $short;
  129. }
  130. private function setLong($long)
  131. {
  132. if (!(is_null($long) || preg_match("/^[a-zA-Z0-9][a-zA-Z0-9_-]{1,}$/", $long))) {
  133. throw new \InvalidArgumentException("Long option must be null or an alphanumeric string, found '$long'");
  134. }
  135. $this->long = $long;
  136. }
  137. private function setMode($mode)
  138. {
  139. if (!in_array($mode, array(Getopt::NO_ARGUMENT, Getopt::OPTIONAL_ARGUMENT, Getopt::REQUIRED_ARGUMENT), true)) {
  140. throw new \InvalidArgumentException("Option mode must be one of "
  141. ."Getopt::NO_ARGUMENT, Getopt::OPTIONAL_ARGUMENT and Getopt::REQUIRED_ARGUMENT");
  142. }
  143. $this->mode = $mode;
  144. }
  145. }