Source for file optlist-defs.php

Documentation is available at optlist-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: optlist-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Handle command-line option arguments conveniently. */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package utils *//**
  27. * A class to make handling command line options a bit easier and more
  28. * consistent.
  29. * We support the following syntaxes:
  30. * programname --myoptname
  31. * programname --myoptname myvalue
  32. * programname --myoptname=myvalue
  33. * programname -t myvalue
  34. * programname --myoptname "myvalue words"
  35. * In the first, only the presence of the option is required, as some kind
  36. * of flag. The second has a value associated with the option, and the
  37. * third is the same but using "=" as the separator. The fourth uses the
  38. * old "-" prefix, and the fifth shows a string as the value.
  39. * @package utils
  40. */
  41. class optlist {
  42. // Public
  43. // Private
  44. /** Array of options
  45. @access private */
  46. var $opts = array();
  47. /** Number of options
  48. @access private */
  49. var $optcount = 0;
  50. /** The current program name
  51. @access private */
  52. var $progname = "";
  53. // .....................................................................
  54. /**
  55. * Instantiate the optlist object. This does all the work since we expect
  56. * that this is being done to process command line arguments/options.
  57. */
  58. function optlist() {
  59. global $argc, $argv;
  60. $this->progname = basename(array_shift($argv));
  61. $argc -= 1;
  62. if (isset($argc) && $argc > 0) {
  63. $optstr = implode(" ", $argv);
  64. $optstr = str_replace("--", "~", $optstr);
  65. $optstr = str_replace(" -", "~", $optstr);
  66. $opts = explode("~", $optstr);
  67. foreach ($opts as $opt) {
  68. if ($opt != "") {
  69. $pos = strpos($opt, "=");
  70. if ($pos > -1) {
  71. $optname = trim(substr($opt, 0, $pos));
  72. $optval = trim(substr($opt, $pos + 1));
  73. }
  74. else {
  75. $pos = strpos($opt, " ");
  76. if ($pos > -1) {
  77. $optname = trim(substr($opt, 0, $pos));
  78. $optval = trim(substr($opt, $pos + 1));
  79. }
  80. else {
  81. $optname = trim($opt);
  82. $optval = "";
  83. }
  84. }
  85. $optval = str_replace("\"", "", $optval);
  86. $optval = str_replace("'", "", $optval);
  87. $this->opts[$optname] = $optval;
  88. }
  89. }
  90. $this->optcount = count($this->opts);
  91. }
  92. } // optlist
  93. // .....................................................................
  94. /**
  95. * Return the value of the named option. Returns the string associated
  96. * with this option, or false if it does not exist.
  97. * @param string $optname Name of the option
  98. * @return mixed The string value of the option, or false if not valid
  99. */
  100. function opt_value($optname) {
  101. if (isset($this->opts[$optname])) {
  102. return $this->opts[$optname];
  103. }
  104. else {
  105. return false;
  106. }
  107. } // opt_value
  108. // .....................................................................
  109. /**
  110. * Return status of the named option. Returns true if the option exists
  111. * or false if it does not exist.
  112. * @param string $optname Name of the option
  113. * @return boolean True if it exists or false if it does not.
  114. */
  115. function opt_exists($optname) {
  116. return (isset($this->opts[$optname]));
  117. } // opt_exists
  118.  
  119. } // optlist class
  120. // -----------------------------------------------------------------------
  121.  
  122. ?>

Documentation generated by phpDocumentor 1.3.0RC3