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

Debug toolbar