ConsoleInputArgument.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /**
  20. * An object to represent a single argument used in the command line.
  21. * ConsoleOptionParser creates these when you use addArgument()
  22. *
  23. * @see ConsoleOptionParser::addArgument()
  24. * @package Cake.Console
  25. */
  26. class ConsoleInputArgument {
  27. /**
  28. * Name of the argument.
  29. *
  30. * @var string
  31. */
  32. protected $_name;
  33. /**
  34. * Help string
  35. *
  36. * @var string
  37. */
  38. protected $_help;
  39. /**
  40. * Is this option required?
  41. *
  42. * @var boolean
  43. */
  44. protected $_required;
  45. /**
  46. * An array of valid choices for this argument.
  47. *
  48. * @var array
  49. */
  50. protected $_choices;
  51. /**
  52. * Make a new Input Argument
  53. *
  54. * @param string|array $name The long name of the option, or an array with all the properties.
  55. * @param string $help The help text for this option
  56. * @param boolean $required Whether this argument is required. Missing required args will trigger exceptions
  57. * @param array $choices Valid choices for this option.
  58. */
  59. public function __construct($name, $help = '', $required = false, $choices = array()) {
  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. return $this->_name;
  78. }
  79. /**
  80. * Generate the help for this argument.
  81. *
  82. * @param integer $width The width to make the name of the option.
  83. * @return string
  84. */
  85. public function help($width = 0) {
  86. $name = $this->_name;
  87. if (strlen($name) < $width) {
  88. $name = str_pad($name, $width, ' ');
  89. }
  90. $optional = '';
  91. if (!$this->isRequired()) {
  92. $optional = __d('cake_console', ' <comment>(optional)</comment>');
  93. }
  94. if (!empty($this->_choices)) {
  95. $optional .= __d('cake_console', ' <comment>(choices: %s)</comment>', implode('|', $this->_choices));
  96. }
  97. return sprintf('%s%s%s', $name, $this->_help, $optional);
  98. }
  99. /**
  100. * Get the usage value for this argument
  101. *
  102. * @return string
  103. */
  104. public function usage() {
  105. $name = $this->_name;
  106. if (!empty($this->_choices)) {
  107. $name = implode('|', $this->_choices);
  108. }
  109. $name = '<' . $name . '>';
  110. if (!$this->isRequired()) {
  111. $name = '[' . $name . ']';
  112. }
  113. return $name;
  114. }
  115. /**
  116. * Check if this argument is a required argument
  117. *
  118. * @return boolean
  119. */
  120. public function isRequired() {
  121. return (bool)$this->_required;
  122. }
  123. /**
  124. * Check that $value is a valid choice for this argument.
  125. *
  126. * @param string $value
  127. * @return boolean
  128. * @throws ConsoleException
  129. */
  130. public function validChoice($value) {
  131. if (empty($this->_choices)) {
  132. return true;
  133. }
  134. if (!in_array($value, $this->_choices)) {
  135. throw new ConsoleException(
  136. __d('cake_console', '"%s" is not a valid value for %s. Please use one of "%s"',
  137. $value, $this->_name, implode(', ', $this->_choices)
  138. ));
  139. }
  140. return true;
  141. }
  142. /**
  143. * Append this arguments XML representation to the passed in SimpleXml object.
  144. *
  145. * @param SimpleXmlElement $parent The parent element.
  146. * @return SimpleXmlElement The parent with this argument appended.
  147. */
  148. public function xml(SimpleXmlElement $parent) {
  149. $option = $parent->addChild('argument');
  150. $option->addAttribute('name', $this->_name);
  151. $option->addAttribute('help', $this->_help);
  152. $option->addAttribute('required', $this->isRequired());
  153. $choices = $option->addChild('choices');
  154. foreach ($this->_choices as $valid) {
  155. $choices->addChild('choice', $valid);
  156. }
  157. return $parent;
  158. }
  159. }