You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Argument.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * This file is part of the PEAR Console_CommandLine package.
  5. *
  6. * PHP version 5
  7. *
  8. * LICENSE: This source file is subject to the MIT license that is available
  9. * through the world-wide-web at the following URI:
  10. * http://opensource.org/licenses/mit-license.php
  11. *
  12. * @category Console
  13. * @package Console_CommandLine
  14. * @author David JEAN LOUIS <izimobil@gmail.com>
  15. * @copyright 2007 David JEAN LOUIS
  16. * @license http://opensource.org/licenses/mit-license.php MIT License
  17. * @version CVS: $Id$
  18. * @link http://pear.php.net/package/Console_CommandLine
  19. * @since File available since release 0.1.0
  20. * @filesource
  21. */
  22. /**
  23. * Include base element class.
  24. */
  25. require_once 'Console/CommandLine/Element.php';
  26. /**
  27. * Class that represent a command line argument.
  28. *
  29. * @category Console
  30. * @package Console_CommandLine
  31. * @author David JEAN LOUIS <izimobil@gmail.com>
  32. * @copyright 2007 David JEAN LOUIS
  33. * @license http://opensource.org/licenses/mit-license.php MIT License
  34. * @version Release: 1.2.2
  35. * @link http://pear.php.net/package/Console_CommandLine
  36. * @since Class available since release 0.1.0
  37. */
  38. class Console_CommandLine_Argument extends Console_CommandLine_Element
  39. {
  40. // Public properties {{{
  41. /**
  42. * Setting this to true will tell the parser that the argument expects more
  43. * than one argument and that argument values should be stored in an array.
  44. *
  45. * @var boolean $multiple Whether the argument expects multiple values
  46. */
  47. public $multiple = false;
  48. /**
  49. * Setting this to true will tell the parser that the argument is optional
  50. * and can be ommited.
  51. * Note that it is not a good practice to make arguments optional, it is
  52. * the role of the options to be optional, by essence.
  53. *
  54. * @var boolean $optional Whether the argument is optional or not.
  55. */
  56. public $optional = false;
  57. /**
  58. * An array of possible values for the argument.
  59. *
  60. * @var array $choices Valid choices for the argument
  61. */
  62. public $choices = array();
  63. // }}}
  64. // validate() {{{
  65. /**
  66. * Validates the argument instance.
  67. *
  68. * @return void
  69. * @throws Console_CommandLine_Exception
  70. * @todo use exceptions
  71. */
  72. public function validate()
  73. {
  74. // check if the argument name is valid
  75. if (!preg_match('/^[a-zA-Z_\x7f-\xff]+[a-zA-Z0-9_\x7f-\xff]*$/',
  76. $this->name)) {
  77. Console_CommandLine::triggerError(
  78. 'argument_bad_name',
  79. E_USER_ERROR,
  80. array('{$name}' => $this->name)
  81. );
  82. }
  83. if (!$this->optional && $this->default !== null) {
  84. Console_CommandLine::triggerError(
  85. 'argument_no_default',
  86. E_USER_ERROR
  87. );
  88. }
  89. parent::validate();
  90. }
  91. // }}}
  92. }