ConsoleInputArgument.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Console;
  16. use Cake\Console\Exception\ConsoleException;
  17. use SimpleXmlElement;
  18. /**
  19. * An object to represent a single argument used in the command line.
  20. * ConsoleOptionParser creates these when you use addArgument()
  21. *
  22. * @see \Cake\Console\ConsoleOptionParser::addArgument()
  23. */
  24. class ConsoleInputArgument
  25. {
  26. /**
  27. * Name of the argument.
  28. *
  29. * @var string
  30. */
  31. protected $_name;
  32. /**
  33. * Help string
  34. *
  35. * @var string
  36. */
  37. protected $_help;
  38. /**
  39. * Is this option required?
  40. *
  41. * @var bool
  42. */
  43. protected $_required;
  44. /**
  45. * An array of valid choices for this argument.
  46. *
  47. * @var array
  48. */
  49. protected $_choices;
  50. /**
  51. * Make a new Input Argument
  52. *
  53. * @param string|array $name The long name of the option, or an array with all the properties.
  54. * @param string $help The help text for this option
  55. * @param bool $required Whether this argument is required. Missing required args will trigger exceptions
  56. * @param array $choices Valid choices for this option.
  57. */
  58. public function __construct($name, $help = '', $required = false, $choices = [])
  59. {
  60. if (is_array($name) && isset($name['name'])) {
  61. foreach ($name as $key => $value) {
  62. $this->{'_' . $key} = $value;
  63. }
  64. } else {
  65. $this->_name = $name;
  66. $this->_help = $help;
  67. $this->_required = $required;
  68. $this->_choices = $choices;
  69. }
  70. }
  71. /**
  72. * Get the value of the name attribute.
  73. *
  74. * @return string Value of this->_name.
  75. */
  76. public function name()
  77. {
  78. return $this->_name;
  79. }
  80. /**
  81. * Checks if this argument is equal to another argument.
  82. *
  83. * @param \Cake\Console\ConsoleInputArgument $argument ConsoleInputArgument to compare to.
  84. * @return bool
  85. */
  86. public function isEqualTo(ConsoleInputArgument $argument)
  87. {
  88. return $this->usage() === $argument->usage();
  89. }
  90. /**
  91. * Generate the help for this argument.
  92. *
  93. * @param int $width The width to make the name of the option.
  94. * @return string
  95. */
  96. public function help($width = 0)
  97. {
  98. $name = $this->_name;
  99. if (strlen($name) < $width) {
  100. $name = str_pad($name, $width, ' ');
  101. }
  102. $optional = '';
  103. if (!$this->isRequired()) {
  104. $optional = ' <comment>(optional)</comment>';
  105. }
  106. if ($this->_choices) {
  107. $optional .= sprintf(' <comment>(choices: %s)</comment>', implode('|', $this->_choices));
  108. }
  109. return sprintf('%s%s%s', $name, $this->_help, $optional);
  110. }
  111. /**
  112. * Get the usage value for this argument
  113. *
  114. * @return string
  115. */
  116. public function usage()
  117. {
  118. $name = $this->_name;
  119. if ($this->_choices) {
  120. $name = implode('|', $this->_choices);
  121. }
  122. $name = '<' . $name . '>';
  123. if (!$this->isRequired()) {
  124. $name = '[' . $name . ']';
  125. }
  126. return $name;
  127. }
  128. /**
  129. * Check if this argument is a required argument
  130. *
  131. * @return bool
  132. */
  133. public function isRequired()
  134. {
  135. return (bool)$this->_required;
  136. }
  137. /**
  138. * Check that $value is a valid choice for this argument.
  139. *
  140. * @param string $value The choice to validate.
  141. * @return bool
  142. * @throws \Cake\Console\Exception\ConsoleException
  143. */
  144. public function validChoice($value)
  145. {
  146. if (empty($this->_choices)) {
  147. return true;
  148. }
  149. if (!in_array($value, $this->_choices)) {
  150. throw new ConsoleException(
  151. sprintf(
  152. '"%s" is not a valid value for %s. Please use one of "%s"',
  153. $value,
  154. $this->_name,
  155. implode(', ', $this->_choices)
  156. )
  157. );
  158. }
  159. return true;
  160. }
  161. /**
  162. * Append this arguments XML representation to the passed in SimpleXml object.
  163. *
  164. * @param \SimpleXmlElement $parent The parent element.
  165. * @return \SimpleXmlElement The parent with this argument appended.
  166. */
  167. public function xml(SimpleXmlElement $parent)
  168. {
  169. $option = $parent->addChild('argument');
  170. $option->addAttribute('name', $this->_name);
  171. $option->addAttribute('help', $this->_help);
  172. $option->addAttribute('required', $this->isRequired());
  173. $choices = $option->addChild('choices');
  174. foreach ($this->_choices as $valid) {
  175. $choices->addChild('choice', $valid);
  176. }
  177. return $parent;
  178. }
  179. }