Option.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace GetOpt;
  3. use GetOpt\ArgumentException\Invalid;
  4. use GetOpt\ArgumentException\Missing;
  5. /**
  6. * Represents an option that GetOpt accepts.
  7. *
  8. * @package GetOpt
  9. * @author Ulrich Schmidt-Goertz
  10. */
  11. class Option implements Describable
  12. {
  13. use WithMagicGetter;
  14. const CLASSNAME = __CLASS__;
  15. private $short;
  16. private $long;
  17. private $mode;
  18. private $description = '';
  19. private $argument;
  20. private $value = null;
  21. /**
  22. * Creates a new option.
  23. *
  24. * @param string $short The option's short name (one of [a-zA-Z0-9?!§$%#]) or null for long-only options
  25. * @param string $long The option's long name (a string of 1+ letter/digit/_/- characters, starting with a letter
  26. * or digit) or null for short-only options
  27. * @param string $mode Whether the option can/must have an argument (optional, defaults to no argument)
  28. */
  29. public function __construct($short, $long = null, $mode = GetOpt::NO_ARGUMENT)
  30. {
  31. if (!$short && !$long) {
  32. throw new \InvalidArgumentException("The short and long name may not both be empty");
  33. }
  34. if ($short == $long) {
  35. throw new \InvalidArgumentException("The short and long names have to be unique");
  36. }
  37. $this->setShort($short);
  38. $this->setLong($long);
  39. $this->setMode($mode);
  40. $this->argument = new Argument();
  41. $this->argument->multiple($this->mode === GetOpt::MULTIPLE_ARGUMENT);
  42. $this->argument->setOption($this);
  43. }
  44. /**
  45. * Fluent interface for constructor so options can be added during construction
  46. *
  47. * @see Options::__construct()
  48. * @param string $short
  49. * @param string $long
  50. * @param string $mode
  51. * @return static
  52. */
  53. public static function create($short, $long = null, $mode = GetOpt::NO_ARGUMENT)
  54. {
  55. return new static($short, $long, $mode);
  56. }
  57. /**
  58. * Defines a description for the option. This is only used for generating usage information.
  59. *
  60. * @param string $description
  61. * @return Option this object (for chaining calls)
  62. */
  63. public function setDescription($description)
  64. {
  65. $this->description = $description;
  66. return $this;
  67. }
  68. /**
  69. * @return string
  70. */
  71. public function getDescription()
  72. {
  73. return $this->description;
  74. }
  75. /**
  76. * @deprecated will be removed in version 4
  77. * @see getDescription
  78. * @codeCoverageIgnore
  79. */
  80. public function description()
  81. {
  82. return $this->description;
  83. }
  84. /**
  85. * Defines a default value for the option.
  86. *
  87. * @param mixed $value
  88. * @return Option this object (for chaining calls)
  89. */
  90. public function setDefaultValue($value)
  91. {
  92. $this->argument->setDefaultValue($value);
  93. return $this;
  94. }
  95. /**
  96. * Defines a validation function for the option.
  97. *
  98. * @param callable $function
  99. * @param string|callable $message
  100. * @return Option this object (for chaining calls)
  101. */
  102. public function setValidation($function, $message = null)
  103. {
  104. $this->argument->setValidation($function, $message);
  105. return $this;
  106. }
  107. /**
  108. * Set the argumentName.
  109. *
  110. * @param $name
  111. * @return $this
  112. */
  113. public function setArgumentName($name)
  114. {
  115. $this->argument->setName($name);
  116. return $this;
  117. }
  118. /**
  119. * Sets the argument object directly.
  120. *
  121. * @param Argument $arg
  122. * @return Option this object (for chaining calls)
  123. */
  124. public function setArgument(Argument $arg)
  125. {
  126. if ($this->mode == GetOpt::NO_ARGUMENT) {
  127. throw new \InvalidArgumentException("Option should not have any argument");
  128. }
  129. $this->argument = clone $arg; // he can reuse his arg but we need a unique arg
  130. $this->argument->multiple($this->mode === GetOpt::MULTIPLE_ARGUMENT);
  131. $this->argument->setOption($this);
  132. return $this;
  133. }
  134. /**
  135. * Change the short name
  136. *
  137. * @param string $short
  138. * @return Option this object (for chaining calls)
  139. */
  140. public function setShort($short)
  141. {
  142. if (!(is_null($short) || preg_match("/^[a-zA-Z0-9?!§$%#]$/", $short))) {
  143. throw new \InvalidArgumentException(sprintf(
  144. 'Short option must be null or one of [a-zA-Z0-9?!§$%%#], found \'%s\'',
  145. $short
  146. ));
  147. }
  148. $this->short = $short;
  149. return $this;
  150. }
  151. /**
  152. * @return string
  153. */
  154. public function getShort()
  155. {
  156. return $this->short;
  157. }
  158. /**
  159. * Returns long name or short name if long name is not set
  160. *
  161. * @return string
  162. */
  163. public function getName()
  164. {
  165. return $this->getLong() ?: $this->getShort();
  166. }
  167. /**
  168. * @deprecated will be removed in version 4
  169. * @see getShort
  170. * @codeCoverageIgnore
  171. */
  172. public function short()
  173. {
  174. return $this->short;
  175. }
  176. /**
  177. * Change the long name
  178. *
  179. * @param $long
  180. * @return Option this object (for chaining calls)
  181. */
  182. public function setLong($long)
  183. {
  184. if (!(is_null($long) || preg_match("/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/", $long))) {
  185. throw new \InvalidArgumentException(sprintf(
  186. 'Long option must be null or an alphanumeric string, found \'%s\'',
  187. $long
  188. ));
  189. }
  190. $this->long = $long;
  191. return $this;
  192. }
  193. /**
  194. * @return string
  195. */
  196. public function getLong()
  197. {
  198. return $this->long;
  199. }
  200. /**
  201. * @deprecated will be removed in version 4
  202. * @see getLong
  203. * @codeCoverageIgnore
  204. */
  205. public function long()
  206. {
  207. return $this->long;
  208. }
  209. /**
  210. * Change the mode
  211. *
  212. * @param $mode
  213. * @return Option this object (for chaining calls)
  214. */
  215. public function setMode($mode)
  216. {
  217. if (!in_array($mode, [
  218. GetOpt::NO_ARGUMENT,
  219. GetOpt::OPTIONAL_ARGUMENT,
  220. GetOpt::REQUIRED_ARGUMENT,
  221. GetOpt::MULTIPLE_ARGUMENT,
  222. ], true)) {
  223. throw new \InvalidArgumentException(sprintf(
  224. 'Option mode must be one of %s, %s, %s and %s',
  225. 'GetOpt::NO_ARGUMENT',
  226. 'GetOpt::OPTIONAL_ARGUMENT',
  227. 'GetOpt::REQUIRED_ARGUMENT',
  228. 'GetOpt::MULTIPLE_ARGUMENT'
  229. ));
  230. }
  231. $this->mode = $mode;
  232. return $this;
  233. }
  234. /**
  235. * @return mixed
  236. */
  237. public function getMode()
  238. {
  239. return $this->mode;
  240. }
  241. /**
  242. * @deprecated will be removed in version 4
  243. * @see getMode
  244. * @codeCoverageIgnore
  245. */
  246. public function mode()
  247. {
  248. return $this->mode;
  249. }
  250. /**
  251. * Retrieve the argument object
  252. *
  253. * @return Argument
  254. */
  255. public function getArgument()
  256. {
  257. return $this->argument;
  258. }
  259. /**
  260. * Internal method to set the current value
  261. *
  262. * @param mixed $value
  263. * @return $this
  264. */
  265. public function setValue($value = null)
  266. {
  267. if ($value === null) {
  268. if (in_array($this->mode, [ GetOpt::REQUIRED_ARGUMENT, GetOpt::MULTIPLE_ARGUMENT ])) {
  269. throw new Missing(sprintf(
  270. GetOpt::translate('option-argument-missing'),
  271. $this->getName()
  272. ));
  273. }
  274. $value = $this->argument->getValue() +1;
  275. }
  276. $this->argument->setValue($value);
  277. return $this;
  278. }
  279. /**
  280. * Get the current value
  281. *
  282. * @return mixed
  283. */
  284. public function getValue()
  285. {
  286. $value = $this->argument->getValue();
  287. return $value === null || $value === [] ? $this->argument->getDefaultValue() : $value;
  288. }
  289. /**
  290. * @deprecated will be removed in version 4
  291. * @see getValue
  292. * @codeCoverageIgnore
  293. */
  294. public function value()
  295. {
  296. return $this->getValue();
  297. }
  298. /**
  299. * Get a string from value
  300. *
  301. * @return string
  302. */
  303. public function __toString()
  304. {
  305. $value = $this->getValue();
  306. return !is_array($value) ? (string)$value : implode(',', $value);
  307. }
  308. /**
  309. * Returns a human readable string representation of the object
  310. *
  311. * @return string
  312. */
  313. public function describe()
  314. {
  315. return sprintf('%s \'%s\'', GetOpt::translate('option'), $this->getName());
  316. }
  317. }