sfCommandOptionSet.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Represent a set of command line options.
  11. *
  12. * @package symfony
  13. * @subpackage command
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfCommandOptionSet.class.php 9076 2008-05-19 23:19:44Z Carl.Vondrick $
  16. */
  17. class sfCommandOptionSet
  18. {
  19. protected
  20. $options = array(),
  21. $shortcuts = array();
  22. /**
  23. * Constructor.
  24. *
  25. * @param array $options An array of sfCommandOption objects
  26. */
  27. public function __construct($options = array())
  28. {
  29. $this->setOptions($options);
  30. }
  31. /**
  32. * Sets the sfCommandOption objects.
  33. *
  34. * @param array $options An array of sfCommandOption objects
  35. */
  36. public function setOptions($options = array())
  37. {
  38. $this->options = array();
  39. $this->shortcuts = array();
  40. $this->addOptions($options);
  41. }
  42. /**
  43. * Add an array of sfCommandOption objects.
  44. *
  45. * @param array $options An array of sfCommandOption objects
  46. */
  47. public function addOptions($options = array())
  48. {
  49. foreach ($options as $option)
  50. {
  51. $this->addOption($option);
  52. }
  53. }
  54. /**
  55. * Add a sfCommandOption objects.
  56. *
  57. * @param sfCommandOption $option A sfCommandOption object
  58. */
  59. public function addOption(sfCommandOption $option)
  60. {
  61. if (isset($this->options[$option->getName()]))
  62. {
  63. throw new sfCommandException(sprintf('An option named "%s" already exist.', $option->getName()));
  64. }
  65. else if (isset($this->shortcuts[$option->getShortcut()]))
  66. {
  67. throw new sfCommandException(sprintf('An option with shortcut "%s" already exist.', $option->getShortcut()));
  68. }
  69. $this->options[$option->getName()] = $option;
  70. if ($option->getShortcut())
  71. {
  72. $this->shortcuts[$option->getShortcut()] = $option->getName();
  73. }
  74. }
  75. /**
  76. * Returns an option by name.
  77. *
  78. * @param string $name The option name
  79. *
  80. * @return sfCommandOption A sfCommandOption object
  81. */
  82. public function getOption($name)
  83. {
  84. if (!$this->hasOption($name))
  85. {
  86. throw new sfCommandException(sprintf('The "--%s" option does not exist.', $name));
  87. }
  88. return $this->options[$name];
  89. }
  90. /**
  91. * Returns true if an option object exists by name.
  92. *
  93. * @param string $name The option name
  94. *
  95. * @return Boolean true if the option object exists, false otherwise
  96. */
  97. public function hasOption($name)
  98. {
  99. return isset($this->options[$name]);
  100. }
  101. /**
  102. * Gets the array of sfCommandOption objects.
  103. *
  104. * @return array An array of sfCommandOption objects
  105. */
  106. public function getOptions()
  107. {
  108. return $this->options;
  109. }
  110. /**
  111. * Returns true if an option object exists by shortcut.
  112. *
  113. * @param string $name The option shortcut
  114. *
  115. * @return Boolean true if the option object exists, false otherwise
  116. */
  117. public function hasShortcut($name)
  118. {
  119. return isset($this->shortcuts[$name]);
  120. }
  121. /**
  122. * Gets an option by shortcut.
  123. *
  124. * @return sfCommandOption A sfCommandOption object
  125. */
  126. public function getOptionForShortcut($shortcut)
  127. {
  128. return $this->getOption($this->shortcutToName($shortcut));
  129. }
  130. /**
  131. * Gets an array of default values.
  132. *
  133. * @return array An array of all default values
  134. */
  135. public function getDefaults()
  136. {
  137. $values = array();
  138. foreach ($this->options as $option)
  139. {
  140. $values[$option->getName()] = $option->getDefault();
  141. }
  142. return $values;
  143. }
  144. /**
  145. * Returns the option name given a shortcut.
  146. *
  147. * @param string $shortcut The shortcut
  148. *
  149. * @return string The option name
  150. */
  151. protected function shortcutToName($shortcut)
  152. {
  153. if (!isset($this->shortcuts[$shortcut]))
  154. {
  155. throw new sfCommandException(sprintf('The "-%s" option does not exist.', $shortcut));
  156. }
  157. return $this->shortcuts[$shortcut];
  158. }
  159. }