PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/parseCommandLineOptions.php

https://code.google.com/p/yapeal/
PHP | 140 lines | 78 code | 0 blank | 62 comment | 15 complexity | b52c1c1ed23630506022f48086889e92 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Contains parseCommandLineOptions 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 used to parser command line options.
  52. *
  53. * This function was made to save having to make new one every time it might be
  54. * needed. It also helps with consistent option handle which should lead to less
  55. * user as well as programmer confusion.
  56. *
  57. * The -h, --help and -V, --version options don't have to be included in the
  58. * parameters as they will always be include automatically.
  59. *
  60. * Note that with older versions of PHP long options aren't available so all
  61. * required options must have a short form that can be used.
  62. *
  63. * @param array $shortOptions An array of short options to accept. The elements
  64. * should be in the same format as the short option parameter for setopt() i.e.
  65. * can be a single character followed by optional single colon for options that
  66. * have required values or double colons for ones that take an optional value.
  67. * @param array $longOptions A simple array of long option names to accept. The
  68. * same options as setopt() work i.e. single colon for required values and
  69. * double colons for ones that take a optional value.
  70. *
  71. * @return array Returns an array of options or an empty array.
  72. *
  73. * @todo Look at making this into a full class instead of just a function.
  74. */
  75. function parseCommandLineOptions(array $shortOptions = NULL,
  76. array $longOptions = NULL) {
  77. if (!function_exists('getopt')) {
  78. return array();
  79. };
  80. $shortOptions = array_merge($shortOptions, array('h', 'V'));
  81. $shortOptions = implode('', $shortOptions);
  82. $longOptions = array_merge($longOptions, array('help', 'version'));
  83. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  84. $options = getopt($shortOptions, $longOptions);
  85. } else {
  86. $options = getopt($shortOptions);
  87. };
  88. $settings = array();
  89. if (empty($options)) {
  90. return $settings;
  91. };
  92. $optionsOnlyMap = array(
  93. 'h' => 'help', 'help' => 'help',
  94. 'V' => 'version', 'version' => 'version'
  95. );
  96. $optionsWithValuesMap = array(
  97. 'c' => 'config', 'config' => 'config',
  98. 'd' => 'database', 'database' => 'database',
  99. 'driver' => 'driver',
  100. 'l' => 'log-config', 'log' => 'log-config',
  101. 'p' => 'password', 'password' => 'password',
  102. 's' => 'host', 'server' =>'host',
  103. 'suffix' => 'suffix',
  104. 'table-prefix' => 'table_prefix',
  105. 'u' => 'username', 'username' => 'username'
  106. );
  107. $optionsWithListMap = array(
  108. 'privileges' => 'privileges',
  109. 'xml' => 'xml'
  110. );
  111. foreach ($options as $opt => $value) {
  112. if (array_key_exists($opt, $optionsOnlyMap)) {
  113. $settings[$optionsOnlyMap[$opt]] = TRUE;
  114. continue;
  115. };
  116. if (array_key_exists($opt, $optionsWithValuesMap)) {
  117. if (is_array($value)) {
  118. // If option is used multiple times use the last value.
  119. $value = $value[count($value) - 1];
  120. };
  121. $settings[$optionsWithValuesMap[$opt]] = (string)$value;
  122. continue;
  123. };// if ... $optionsWithValuesMap ...
  124. if (array_key_exists($opt, $optionsWithListMap)) {
  125. if (is_array($value)) {
  126. // If option is used multiple times combined them.
  127. $value = implode(' ', $value);
  128. };
  129. if (isset($settings[$optionsWithListMap[$opt]])) {
  130. // Append to the existing list from short option.
  131. $settings[$optionsWithListMap[$opt]] .= ' ' . $value;
  132. } else {
  133. $settings[$optionsWithListMap[$opt]] = $value;
  134. };
  135. continue;
  136. };// if ... $optionsWithListMap ...
  137. };// foreach $options...
  138. return $settings;
  139. }// function parseCommandLineOptions
  140. ?>