PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/usage.php

https://code.google.com/p/yapeal/
PHP | 174 lines | 122 code | 0 blank | 52 comment | 11 complexity | 4b52e338683b59ba2c876fffdef9f6e0 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Contains usage function.
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE: This file is part of Yet Another Php Eve Api library also know
  8. * as Yapeal which will be used to refer to it in the rest of this license.
  9. *
  10. * Yapeal is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Yapeal is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with Yapeal. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @author Michael Cummings <mgcummings@yahoo.com>
  24. * @copyright Copyright (c) 2008-2012, Michael Cummings
  25. * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
  26. * @package Yapeal
  27. * @link http://code.google.com/p/yapeal/
  28. * @link http://www.eveonline.com/
  29. */
  30. /**
  31. * @internal Allow viewing of the source code in web browser.
  32. */
  33. if (isset($_REQUEST['viewSource'])) {
  34. highlight_file(__FILE__);
  35. exit();
  36. };
  37. /**
  38. * @internal Only let this code be included.
  39. */
  40. if (count(get_included_files()) < 2) {
  41. $mess = basename(__FILE__)
  42. . ' must be included it can not be ran directly.' . PHP_EOL;
  43. if (PHP_SAPI != 'cli') {
  44. header('HTTP/1.0 403 Forbidden', TRUE, 403);
  45. die($mess);
  46. };
  47. fwrite(STDERR, $mess);
  48. exit(1);
  49. };
  50. /**
  51. * Function use to show the usage message on command line.
  52. *
  53. * Note that the 'h' and 'V' options and their corresponding long versions will
  54. * be included automatically without having to add them to the parameters.
  55. *
  56. * @param string $file Name of script file.
  57. * @param array $shortOptions An array of short options to accept. The elements
  58. * should be in the same format as the short option parameter for setopt() i.e.
  59. * can be a single character followed by optional single colon for options that
  60. * have required values or double colons for ones that take an optional value.
  61. * @param array $longOptions A simple array of long option names to accept. The
  62. * same options as setopt() work i.e. single colon for required values and
  63. * double colons for ones that take a optional value.
  64. *
  65. */
  66. function usage($file, array $shortOptions = NULL, array $longOptions = NULL) {
  67. $shortOptions = array_merge($shortOptions, array('h', 'V'));
  68. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  69. $longOptions = array_merge($longOptions, array('help', 'version'));
  70. } else {
  71. $longOptions = array();
  72. };
  73. $file = basename($file);
  74. $cutLine = 78;
  75. $ragLine = $cutLine - 5;
  76. $mess = PHP_EOL . 'Usage: ' . $file;
  77. $mess .= ' [[-h | -V] | Options ...]' . PHP_EOL . PHP_EOL;
  78. $desc = 'The script reads database settings from [Database] section of the';
  79. $desc .= ' configuration file, either the default one in';
  80. $desc .= ' < yapeal_base>/config/yapeal.ini or the custom one from the -c or';
  81. $desc .= ' --config OPTION.';
  82. $desc .= ' Command line options have priority over settings in the';
  83. $desc .= ' configuration file and the configuration file has priority over';
  84. $desc .= ' the defaults. If no configuration file is found, either default';
  85. $desc .= ' or custom, or if some of the settings are missing from it the';
  86. $desc .= ' corresponding command line options becomes required. For example';
  87. $desc .= ' if the configuration file had all but the "password" setting then';
  88. $desc .= ' the -p or --password option would no longer be optional but';
  89. $desc .= ' required on the command line.';
  90. // Make text ragged right with forced word wrap at 80 characters.
  91. $desc = wordwrap($desc, $ragLine, PHP_EOL);
  92. $desc = wordwrap($desc, $cutLine, PHP_EOL, TRUE);
  93. $mess .= $desc . PHP_EOL . PHP_EOL;
  94. $desc = 'Mandatory arguments to long options are mandatory for short options';
  95. $desc .= ' as well. For most OPTIONs if they are used more than once only the';
  96. $desc .= ' last value will be used. Exceptions to this will be noted below.';
  97. $desc = wordwrap($desc, $ragLine, PHP_EOL);
  98. $desc = wordwrap($desc, $cutLine, PHP_EOL, TRUE);
  99. $mess .= $desc . PHP_EOL . PHP_EOL;
  100. $mess .= 'Options:' . PHP_EOL;
  101. $options = array();
  102. $options['c:'] = array('op' => ' -c, --config=FILE', 'desc' =>
  103. 'Read configuration from FILE. This is an optional setting to allow the use'
  104. . ' of a custom configuration file. FILE must be in "ini" format. Defaults'
  105. . ' to <yapeal_base>/config/yapeal.ini.');
  106. $options['d:'] = array('op' => ' -d, --database=DB', 'desc' =>
  107. 'DB is the database name to use for the operation.');
  108. $options['driver:'] = array('op' => ' --driver=DRIVER', 'desc' =>
  109. 'DRIVER is only use during testing and should only be used if directed to'
  110. . ' do so by a developer. Optional setting that defaults to mysql://.');
  111. $options['h'] = array('op' => ' -h, --help', 'desc' => 'Show this help.');
  112. $options['l:'] = array('op' => ' -l, --log-config=LOG', 'desc' =>
  113. 'LOG should be the path and name of a file that holds logging configuration'
  114. . ' settings. The file can be in either INI or XML format. Optional setting'
  115. . ' that defaults to <yapeal_base>/config/logger.xml');
  116. $options['p:'] = array('op' => ' -p, password=PASS', 'desc' =>
  117. 'PASS is the password for the database server.');
  118. $options['privileges:'] = array('op' => ' --privileges=PRIVS', 'desc' =>
  119. 'Optional PRIVS list. It is either a quoted space separated list of'
  120. . ' privileges names to use or can be used multiple times and the values'
  121. . ' from each one will be appended to the list. For example you can either'
  122. . ' do ' . $file . ' --privileges="alter create" OR'
  123. . ' ' . $file . ' --privileges="alter" --privileges="create". This option'
  124. . ' should rarely be needed as Yapeal uses the default list "alter create'
  125. . ' delete drop index insert select" which includes all the privileges'
  126. . ' normally needed.');
  127. $options['s:'] = array('op' => ' -s, --server=HOST', 'desc' =>
  128. 'HOST is the database server name to use.');
  129. $options['suffix:'] =array('op' => ' --suffix=SUFFIX', 'desc' =>
  130. 'SUFFIX is another optional setting only used during testing. Only use if'
  131. . ' directed to by a developer. Defaults to ?new.');
  132. $options['table-prefix:'] = array('op' => ' --table-prefix=PREFIX', 'desc' =>
  133. 'Append PREFIX to all the table names. This is an optional setting that is'
  134. . ' mostly useful when combining Yapeal tables with the tables from an'
  135. . ' application in the same database. Defaults to empty string.');
  136. $options['u:'] = array('op' => ' -u, --username=USER', 'desc' =>
  137. 'USER is the user name for the database server.');
  138. $options['V'] = array('op' => ' -V, --version', 'desc' =>
  139. 'Show version and licensing information.');
  140. $options['xml:'] = array('op' => ' --xml=XML', 'desc' =>
  141. 'Optional XML section list. It is either a quoted space separated list of'
  142. . ' xml section names to use or can be used multiple times and the values'
  143. . ' from each one will be appended to the list. For example you can either'
  144. . ' do ' . $file . ' -xml="util account" OR'
  145. . ' ' . $file . ' -xml="util" -xml="account". This option should rarely be'
  146. . ' needed as Yapeal uses the default list "util account char corp eve map'
  147. . ' server" which includes all the sections normally needed.');
  148. $width = 0;
  149. foreach ($options as $k => $v) {
  150. if (!(in_array($k, $shortOptions) || in_array($k, $longOptions))) {
  151. continue;
  152. };
  153. if (strlen($v['op']) > $width) {
  154. $width = strlen($v['op']);
  155. };
  156. };// foreach $options ...
  157. $width += 4;
  158. $break = PHP_EOL . str_pad('', $width);
  159. $descCut = $cutLine - $width;
  160. $descRag = $descCut - 5;
  161. foreach ($options as $k => $v) {
  162. if (!(in_array($k, $shortOptions) || in_array($k, $longOptions))) {
  163. continue;
  164. };
  165. $option = str_pad($v['op'], $width);
  166. // Make description text ragged right with forced word wrap at full width.
  167. $desc = wordwrap($v['desc'], $descRag, PHP_EOL);
  168. $desc = wordwrap($desc, $descCut, PHP_EOL, TRUE);
  169. $option .= str_replace(PHP_EOL, $break, $desc);
  170. $mess .= $option . PHP_EOL . PHP_EOL;
  171. };// foreach $options ...
  172. fwrite(STDOUT, $mess);
  173. };// function usage
  174. ?>