1. sfCommandArgument.class.php
  2. /** * Represents a command line argument. * * @package symfony * @subpackage command * @author Fabien Potencier * @version SVN: $Id: sfCommandArgument.class.php 21908 2009-09-11 12:06:21Z fabien $ */
  3. class sfCommandArgument
  4. {
  5. const REQUIRED = 1;
  6. const OPTIONAL = 2;
  7. const IS_ARRAY = 4;
  8. protected
  9. $name = null,
  10. $mode = null,
  11. $default = null,
  12. $help = '';
  13. /**
  14. * Constructor.
  15. *
  16. * @param string $name The argument name
  17. * @param integer $mode The argument mode: self::REQUIRED or self::OPTIONAL
  18. * @param string $help A help text
  19. * @param mixed $default The default value (for self::OPTIONAL mode only)
  20. */
  21. public function __construct($name, $mode = null, $help = '', $default = null)
  22. {
  23. if (null === $mode)
  24. {
  25. $mode = self::OPTIONAL;
  26. }
  27. else if (is_string($mode) || $mode > 7)
  28. {
  29. throw new sfCommandException(sprintf('Argument mode "%s" is not valid.', $mode));
  30. }
  31. $this->name = $name;
  32. $this->mode = $mode;
  33. $this->help = $help;
  34. $this->setDefault($default);
  35. }
  36. /**
  37. * Returns the argument name.
  38. *
  39. * @return string The argument name
  40. */
  41. public function getName()
  42. {
  43. return $this->name;
  44. }
  45. /**
  46. * Returns true if the argument is required.
  47. *
  48. * @return Boolean true if parameter mode is self::REQUIRED, false otherwise
  49. */
  50. public function isRequired()
  51. {
  52. return self::REQUIRED === (self::REQUIRED & $this->mode);
  53. }
  54. /**
  55. * Returns true if the argument can take multiple values.
  56. *
  57. * @return Boolean true if mode is self::IS_ARRAY, false otherwise
  58. */
  59. public function isArray()
  60. {
  61. return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
  62. }
  63. /**
  64. * Sets the default value.
  65. *
  66. * @param mixed $default The default value
  67. */
  68. public function setDefault($default = null)
  69. {
  70. if (self::REQUIRED === $this->mode && null !== $default)
  71. {
  72. throw new sfCommandException('Cannot set a default value except for sfCommandParameter::OPTIONAL mode.');
  73. }
  74. if ($this->isArray())
  75. {
  76. if (null === $default)
  77. {
  78. $default = array();
  79. }
  80. else if (!is_array($default))
  81. {
  82. throw new sfCommandException('A default value for an array argument must be an array.');
  83. }
  84. }
  85. $this->default = $default;
  86. }
  87. /**
  88. * Returns the default value.
  89. *
  90. * @return mixed The default value
  91. */
  92. public function getDefault()
  93. {
  94. return $this->default;
  95. }
  96. /**
  97. * Returns the help text.
  98. *
  99. * @return string The help text
  100. */
  101. public function getHelp()
  102. {
  103. return $this->help;
  104. }
  105. }

Debug toolbar