Base for a static organization website

ConsoleInputArgument.php 4.1KB

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