PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/Util/Cli/Getopt.php

https://github.com/whiletrue/php-util
PHP | 170 lines | 133 code | 30 blank | 7 comment | 23 complexity | 4eaceaf69debf86efd59104c6193b542 MD5 | raw file
  1. <?php
  2. /**
  3. * Utility Class to handle commandline options
  4. * and arguments
  5. *
  6. * @author <silvan@etoy.com>
  7. */
  8. Class Util_Cli_Getopt {
  9. protected $_opts = array();
  10. protected $_optdef = array();
  11. protected $_args = array();
  12. public function __construct($sOpts='', array $lOpts=array()) {
  13. $this->setOpts($sOpts, $lOpts);
  14. $this->parseOpts();
  15. }
  16. protected function setOpts($shortopts='', array $longopts=array()) {
  17. $this->setShortOpts($shortopts);
  18. $this->setLongOpts($longopts);
  19. }
  20. protected function addOptDef(array $def = array()) {
  21. $this->_optdef = array_merge($this->_optdef, $def);
  22. }
  23. protected function addArg($arg) {
  24. return ($this->_args[] = $arg);
  25. }
  26. protected function addArgs(array $args=array()) {
  27. $this->_args = array_merge($this->_args, $args);
  28. }
  29. protected function setShortOpts($so='') {
  30. $chars = range('a', 'z');
  31. $chr = str_split($so);
  32. $so = array();
  33. $cc = '';
  34. foreach($chr as $i => $c) {
  35. if (in_array($c, $chars)) {
  36. $so[$c] = '';
  37. $cc = $c;
  38. } else if ($c === ":") {
  39. $so[$cc] .= $c;
  40. }
  41. }
  42. $this->addOptDef($so);
  43. }
  44. protected function setLongOpts(array $lo = array()) {
  45. $longOpts = array();
  46. foreach($lo as $i => $opt) {
  47. if (preg_match("#(\w+)(\:{1,2})?#", $opt, $matches)) {
  48. $m = (isset($matches[2])) ? $matches[2] : '';
  49. $longOpts[$matches[1]] = $m;
  50. }
  51. }
  52. $this->addOptDef($longOpts);
  53. return true;
  54. }
  55. protected function getArgv() {
  56. if (!isset($_SERVER['argv'])) {
  57. return false;
  58. }
  59. return array_slice($_SERVER['argv'], 1);
  60. }
  61. protected function parseOpts() {
  62. $argv = $this->getArgv();
  63. if (!$argv) {
  64. return;
  65. }
  66. while(count($argv) > 0) {
  67. $tok = array_shift($argv);
  68. if (substr($tok, 0, 2) === '--') {
  69. $this->parseLongOpt($tok, $argv);
  70. } else if (substr($tok,0, 1) === '-') {
  71. $this->parseShortOpt($tok, $argv);
  72. } else {
  73. // argument
  74. $this->addArg($tok);
  75. }
  76. }
  77. }
  78. protected function parseNextValue(&$argv) {
  79. if (strpos($argv[0], '-') !== false && strpos($argv[0], '--') !== false) {
  80. return false;
  81. }
  82. return $argv[0];
  83. }
  84. protected function parseOptValue($opt, &$argv, $parsed='') {
  85. $cfg = $this->_optdef[$opt];
  86. if ($cfg === '') {
  87. return true;
  88. } else if ($cfg === ':' || $cfg === '::') {
  89. if (!empty($parsed)) {
  90. return $parsed;
  91. }
  92. $v = $this->parseNextValue($argv);
  93. if (false !== $v) {
  94. array_shift($argv);
  95. return $v;
  96. }
  97. }
  98. return false;
  99. }
  100. protected function parseLongOpt($str, &$argv) {
  101. if (preg_match('#--(\w+)[\s\=]?([\w]*)#', $str, $matches) &&
  102. isset($this->_optdef[$matches[1]])) {
  103. $v = $this->parseOptValue($matches[1], $argv, $matches[2]);
  104. if (false !== $v) {
  105. $this->setOpt($matches[1], $v);
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. protected function parseShortOpt($str, &$argv) {
  112. if (preg_match('#-(\w{1})\s?([\w]*)#', $str, $matches) &&
  113. isset($this->_optdef[$matches[1]])) {
  114. $v = $this->parseOptValue($matches[1], $argv, $matches[2]);
  115. if (false !== $v) {
  116. $this->setOpt($matches[1], $v);
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. public function getArgs() {
  123. return $this->_args;
  124. }
  125. public function setOpt($name, $value) {
  126. return ($this->_opts[$name] = $value);
  127. }
  128. public function getOpts() {
  129. return $this->_opts;
  130. }
  131. public function getOpt($oname) {
  132. if (isset($this->_opts[$oname])) {
  133. return $this->_opts[$oname];
  134. }
  135. return Null;
  136. }
  137. public function hasOpt($oname) {
  138. return (isset($this->_opts[$oname]));
  139. }
  140. }