/branches/version1.x/app/Util.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 97 lines · 59 code · 6 blank · 32 comment · 24 complexity · ba7bfdb149411f2f27e230558d59ddee MD5 · raw file

  1. /**
  2. * @fileOverview
  3. * @name Util
  4. * @author Michael Mathews micmath@gmail.com
  5. * @url $HeadURL: http://jsdoc-toolkit.googlecode.com/svn/branches/version1.x/app/Util.js $
  6. * @revision $Id: Util.js 244 2007-09-23 09:42:21Z micmath $
  7. * @license <a href="http://en.wikipedia.org/wiki/MIT_License">X11/MIT License</a>
  8. * (See the accompanying README file for full details.)
  9. */
  10. /**
  11. * @class Various utility methods used by JsDoc.
  12. * @static
  13. */
  14. Util = {
  15. /**
  16. * Turn a path into just the name of the file.
  17. * @static
  18. * @param {string} path
  19. * @return {string} The fileName portion of the path.
  20. */
  21. fileName: function(path) {
  22. var nameStart = Math.max(path.lastIndexOf("/")+1, path.lastIndexOf("\\")+1, 0);
  23. return path.substring(nameStart);
  24. },
  25. /**
  26. * Turn a path into just the directory part.
  27. * @static
  28. * @param {string} path
  29. * @return {string} The directory part of the path.
  30. */
  31. dir: function(path) {
  32. var nameStart = Math.max(path.lastIndexOf("/")+1, path.lastIndexOf("\\")+1, 0);
  33. return path.substring(0, nameStart-1);
  34. },
  35. /**
  36. * Get commandline option values.
  37. * @static
  38. * @param {Array} args Commandline arguments. Like ["-a=xml", "-b", "--class=new", "--debug"]
  39. * @param {object} optNames Map short names to long names. Like {a:"accept", b:"backtrace", c:"class", d:"debug"}.
  40. * @return {object} Short names and values. Like {a:"xml", b:true, c:"new", d:true}
  41. */
  42. getOptions: function(args, optNames) {
  43. var opt = {"_": []}; // the unnamed option allows multiple values
  44. for (var i = 0; i < args.length; i++) {
  45. var arg = new String(args[i]);
  46. var name;
  47. var value;
  48. if (arg.charAt(0) == "-") {
  49. if (arg.charAt(1) == "-") { // it's a longname like --foo
  50. arg = arg.substring(2);
  51. var m = arg.split("=");
  52. name = m.shift();
  53. value = m.shift();
  54. if (typeof value == "undefined") value = true;
  55. for (var n in optNames) { // convert it to a shortname
  56. if (name == optNames[n]) {
  57. name = n;
  58. }
  59. }
  60. }
  61. else { // it's a shortname like -f
  62. arg = arg.substring(1);
  63. var m = arg.split("=");
  64. name = m.shift();
  65. value = m.shift();
  66. if (typeof value == "undefined") value = true;
  67. for (var n in optNames) { // find the matching key
  68. if (name == n || name+'[]' == n) {
  69. name = n;
  70. break;
  71. }
  72. }
  73. }
  74. if (name.match(/(.+)\[\]$/)) { // it's an array type like n[]
  75. name = RegExp.$1;
  76. if (!opt[name]) opt[name] = [];
  77. }
  78. if (opt[name] && opt[name].push) {
  79. opt[name].push(value);
  80. }
  81. else {
  82. opt[name] = value;
  83. }
  84. }
  85. else { // not associated with any optname
  86. opt._.push(args[i]);
  87. }
  88. }
  89. return opt;
  90. }
  91. }