PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/include/class.cli.php

https://gitlab.com/billyprice1/osTicket
PHP | 309 lines | 267 code | 38 blank | 4 comment | 56 complexity | 88b869d631a7d6621c01e7926d7f260b MD5 | raw file
  1. <?php
  2. class Option {
  3. var $default = false;
  4. function __construct($options=false) {
  5. list($this->short, $this->long) = array_slice($options, 0, 2);
  6. $this->help = (isset($options['help'])) ? $options['help'] : "";
  7. $this->action = (isset($options['action'])) ? $options['action']
  8. : "store";
  9. $this->dest = (isset($options['dest'])) ? $options['dest']
  10. : substr($this->long, 2);
  11. $this->type = (isset($options['type'])) ? $options['type']
  12. : 'string';
  13. $this->const = (isset($options['const'])) ? $options['const']
  14. : null;
  15. $this->default = (isset($options['default'])) ? $options['default']
  16. : null;
  17. $this->metavar = (isset($options['metavar'])) ? $options['metavar']
  18. : 'var';
  19. $this->nargs = (isset($options['nargs'])) ? $options['nargs']
  20. : 1;
  21. }
  22. function hasArg() {
  23. return $this->action != 'store_true'
  24. && $this->action != 'store_false';
  25. }
  26. function handleValue(&$destination, $args) {
  27. $nargs = 0;
  28. $value = ($this->hasArg()) ? array_shift($args) : null;
  29. if ($value[0] == '-')
  30. $value = null;
  31. elseif ($value)
  32. $nargs = 1;
  33. switch ($this->type) {
  34. case 'int':
  35. $value = (int)$value;
  36. break;
  37. case 'bool':
  38. $value = strcasecmp($value, 'true') === 0 || ((int) $value);
  39. break;
  40. }
  41. switch ($this->action) {
  42. case 'store_true':
  43. $value = true;
  44. break;
  45. case 'store_false':
  46. $value = false;
  47. break;
  48. case 'store_const':
  49. $value = $this->const;
  50. break;
  51. case 'append':
  52. if (!isset($destination[$this->dest]))
  53. $destination[$this->dest] = array($value);
  54. else {
  55. $T = &$destination[$this->dest];
  56. $T[] = $value;
  57. $value = $T;
  58. }
  59. break;
  60. case 'store':
  61. default:
  62. break;
  63. }
  64. $destination[$this->dest] = $value;
  65. return $nargs;
  66. }
  67. function toString() {
  68. $short = explode(':', $this->short);
  69. $long = explode(':', $this->long);
  70. if ($this->nargs === '?')
  71. $switches = sprintf(' %s [%3$s], %s[=%3$s]', $short[0],
  72. $long[0], $this->metavar);
  73. elseif ($this->hasArg())
  74. $switches = sprintf(' %s %3$s, %s=%3$s', $short[0], $long[0],
  75. $this->metavar);
  76. else
  77. $switches = sprintf(" %s, %s", $short[0], $long[0]);
  78. $help = preg_replace('/\s+/', ' ', $this->help);
  79. if (strlen($switches) > 23)
  80. $help = "\n" . str_repeat(" ", 24) . $help;
  81. else
  82. $switches = str_pad($switches, 24);
  83. $help = wordwrap($help, 54, "\n" . str_repeat(" ", 24));
  84. return $switches . $help;
  85. }
  86. }
  87. class OutputStream {
  88. var $stream;
  89. function __construct($stream) {
  90. if (!($this->stream = fopen($stream, 'w')))
  91. throw new Exception(sprintf('%s: Cannot open for writing',
  92. $stream));
  93. }
  94. function write($what) {
  95. fwrite($this->stream, $what);
  96. }
  97. }
  98. class Module {
  99. var $options = array();
  100. var $arguments = array();
  101. var $prologue = "";
  102. var $epilog = "";
  103. var $usage = '$script [options] $args [arguments]';
  104. var $autohelp = true;
  105. var $module_name;
  106. var $stdout;
  107. var $stderr;
  108. var $_options;
  109. var $_args;
  110. function __construct() {
  111. $this->options['help'] = array("-h","--help",
  112. 'action'=>'store_true',
  113. 'help'=>"Display this help message");
  114. foreach ($this->options as &$opt)
  115. $opt = new Option($opt);
  116. $this->stdout = new OutputStream('php://output');
  117. $this->stderr = new OutputStream('php://stderr');
  118. }
  119. function showHelp() {
  120. if ($this->prologue)
  121. echo $this->prologue . "\n\n";
  122. global $argv;
  123. $manager = @$argv[0];
  124. echo "Usage:\n";
  125. echo " " . str_replace(
  126. array('$script', '$args'),
  127. array($manager ." ". $this->module_name, implode(' ', array_keys($this->arguments))),
  128. $this->usage) . "\n";
  129. ksort($this->options);
  130. if ($this->options) {
  131. echo "\nOptions:\n";
  132. foreach ($this->options as $name=>$opt)
  133. echo $opt->toString() . "\n";
  134. }
  135. if ($this->arguments) {
  136. echo "\nArguments:\n";
  137. foreach ($this->arguments as $name=>$help) {
  138. $extra = '';
  139. if (is_array($help)) {
  140. if (isset($help['options']) && is_array($help['options'])) {
  141. foreach($help['options'] as $op=>$desc)
  142. $extra .= wordwrap(
  143. "\n $op - $desc", 76, "\n ");
  144. }
  145. $help = $help['help'];
  146. }
  147. echo $name . "\n " . wordwrap(
  148. preg_replace('/\s+/', ' ', $help), 76, "\n ")
  149. .$extra."\n";
  150. }
  151. }
  152. if ($this->epilog) {
  153. echo "\n\n";
  154. $epilog = preg_replace('/\s+/', ' ', $this->epilog);
  155. echo wordwrap($epilog, 76, "\n");
  156. }
  157. echo "\n";
  158. }
  159. function getOption($name, $default=false) {
  160. $this->parseOptions();
  161. if (isset($this->_options[$name]))
  162. return $this->_options[$name];
  163. elseif (isset($this->options[$name]) && $this->options[$name]->default)
  164. return $this->options[$name]->default;
  165. else
  166. return $default;
  167. }
  168. function getArgument($name, $default=false) {
  169. $this->parseOptions();
  170. if (isset($this->_args[$name]))
  171. return $this->_args[$name];
  172. return $default;
  173. }
  174. function parseOptions() {
  175. if (is_array($this->_options))
  176. return;
  177. global $argv;
  178. list($this->_options, $this->_args) =
  179. $this->parseArgs(array_slice($argv, 1));
  180. foreach (array_keys($this->arguments) as $idx=>$name)
  181. if (!isset($this->_args[$idx])) {
  182. $info = $this->arguments[$name];
  183. if (!is_array($info) || !isset($info['required']) || $info['required'])
  184. $this->optionError($name . " is a required argument");
  185. }
  186. elseif (is_array($this->arguments[$name])
  187. && isset($this->arguments[$name]['options'])
  188. && !isset($this->arguments[$name]['options'][$this->_args[$idx]]))
  189. $this->optionError($name . " does not support such a value");
  190. else
  191. $this->_args[$name] = &$this->_args[$idx];
  192. foreach ($this->options as $name=>$opt)
  193. if (!isset($this->_options[$name]))
  194. $this->_options[$name] = $opt->default;
  195. if ($this->autohelp && $this->getOption('help')) {
  196. $this->showHelp();
  197. die();
  198. }
  199. }
  200. function optionError($error) {
  201. echo "Error: " . $error . "\n\n";
  202. $this->showHelp();
  203. die();
  204. }
  205. function _run($module_name, $bootstrap=true) {
  206. $this->module_name = $module_name;
  207. if ($bootstrap)
  208. $this->bootstrap();
  209. $this->parseOptions();
  210. return $this->run($this->_args, $this->_options);
  211. }
  212. /* abstract */
  213. function run($args, $options) {
  214. }
  215. function bootstrap() {
  216. Bootstrap::loadConfig();
  217. Bootstrap::defineTables(TABLE_PREFIX);
  218. Bootstrap::loadCode();
  219. }
  220. function fail($message) {
  221. $this->stderr->write($message . "\n");
  222. die();
  223. }
  224. /* static */
  225. function register($action, $class) {
  226. global $registered_modules;
  227. $registered_modules[$action] = new $class();
  228. }
  229. /* static */ function getInstance($action) {
  230. global $registered_modules;
  231. return $registered_modules[$action];
  232. }
  233. function parseArgs($argv) {
  234. $options = $args = array();
  235. $argv = array_slice($argv, 0);
  236. $more_opts = true;
  237. while ($arg = array_shift($argv)) {
  238. if (strpos($arg, '=') !== false) {
  239. list($arg, $value) = explode('=', $arg, 2);
  240. array_unshift($argv, $value);
  241. }
  242. if ($arg == '--') {
  243. $more_opts = false;
  244. continue;
  245. }
  246. // Allow multiple simple args like -Dvt
  247. if ($arg[0] == '-' && $arg[1] != '-' && strlen($arg) > 2) {
  248. foreach (str_split(substr($arg, 2)) as $O)
  249. array_unshift($argv, "-{$O}");
  250. $arg = substr($arg, 0, 2);
  251. }
  252. $found = false;
  253. if ($more_opts && $arg[0] == '-') {
  254. foreach ($this->options as $opt) {
  255. if ($opt->short == $arg || $opt->long == $arg) {
  256. if ($nargs = $opt->handleValue($options, $argv))
  257. while ($nargs--)
  258. array_shift($argv);
  259. $found = true;
  260. }
  261. }
  262. }
  263. if (!$found && (!$more_opts || $arg[0] != '-'))
  264. $args[] = $arg;
  265. // XXX else show help if $strict?
  266. }
  267. return array($options, $args);
  268. }
  269. }
  270. $registered_modules = array();
  271. ?>