PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Console/ConsoleInputArgument.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 168 lines | 73 code | 10 blank | 85 comment | 10 complexity | 9926506e2e6715ba2f515a0cb672d35b MD5 | raw file
  1. <?php
  2. /**
  3. * ConsoleArgumentOption file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since CakePHP(tm) v 2.0
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  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 ConsoleOptionParser::addArgument()
  23. * @package Cake.Console
  24. */
  25. class ConsoleInputArgument {
  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 boolean
  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 mixed $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 boolean $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 = array()) {
  59. if (is_array($name) && isset($name['name'])) {
  60. foreach ($name as $key => $value) {
  61. $this->{'_' . $key} = $value;
  62. }
  63. } else {
  64. $this->_name = $name;
  65. $this->_help = $help;
  66. $this->_required = $required;
  67. $this->_choices = $choices;
  68. }
  69. }
  70. /**
  71. * Get the value of the name attribute.
  72. *
  73. * @return string Value of this->_name.
  74. */
  75. public function name() {
  76. return $this->_name;
  77. }
  78. /**
  79. * Generate the help for this argument.
  80. *
  81. * @param integer $width The width to make the name of the option.
  82. * @return string
  83. */
  84. public function help($width = 0) {
  85. $name = $this->_name;
  86. if (strlen($name) < $width) {
  87. $name = str_pad($name, $width, ' ');
  88. }
  89. $optional = '';
  90. if (!$this->isRequired()) {
  91. $optional = __d('cake_console', ' <comment>(optional)</comment>');
  92. }
  93. if (!empty($this->_choices)) {
  94. $optional .= __d('cake_console', ' <comment>(choices: %s)</comment>', implode('|', $this->_choices));
  95. }
  96. return sprintf('%s%s%s', $name, $this->_help, $optional);
  97. }
  98. /**
  99. * Get the usage value for this argument
  100. *
  101. * @return string
  102. */
  103. public function usage() {
  104. $name = $this->_name;
  105. if (!empty($this->_choices)) {
  106. $name = implode('|', $this->_choices);
  107. }
  108. $name = '<' . $name . '>';
  109. if (!$this->isRequired()) {
  110. $name = '[' . $name . ']';
  111. }
  112. return $name;
  113. }
  114. /**
  115. * Check if this argument is a required argument
  116. *
  117. * @return boolean
  118. */
  119. public function isRequired() {
  120. return (bool) $this->_required;
  121. }
  122. /**
  123. * Check that $value is a valid choice for this argument.
  124. *
  125. * @param string $value
  126. * @return boolean
  127. * @throws ConsoleException
  128. */
  129. public function validChoice($value) {
  130. if (empty($this->_choices)) {
  131. return true;
  132. }
  133. if (!in_array($value, $this->_choices)) {
  134. throw new ConsoleException(
  135. __d('cake_console', '"%s" is not a valid value for %s. Please use one of "%s"',
  136. $value, $this->_name, implode(', ', $this->_choices)
  137. ));
  138. }
  139. return true;
  140. }
  141. /**
  142. * Append this arguments XML representation to the passed in SimpleXml object.
  143. *
  144. * @param SimpleXmlElement $parent The parent element.
  145. * @return SimpleXmlElement The parent with this argument appended.
  146. */
  147. public function xml(SimpleXmlElement $parent) {
  148. $option = $parent->addChild('argument');
  149. $option->addAttribute('name', $this->_name);
  150. $option->addAttribute('help', $this->_help);
  151. $option->addAttribute('required', $this->isRequired());
  152. $choices = $option->addChild('choices');
  153. foreach ($this->_choices as $valid) {
  154. $choices->addChild('choice', $valid);
  155. }
  156. return $parent;
  157. }
  158. }