ConsoleInputArgument.php 4.1 KB

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