PageRenderTime 39ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/jsdoc_toolkit-2.2.1/jsdoc-toolkit/app/frame/Opt.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 134 lines | 51 code | 4 blank | 79 comment | 24 complexity | b569893fd4d68e2f858babd68da628d7 MD5 | raw file
  1. /** @namespace */
  2. Opt = {
  3. /**
  4. * Get commandline option values.
  5. * @param {Array} args Commandline arguments. Like ["-a=xml", "-b", "--class=new", "--debug"]
  6. * @param {object} optNames Map short names to long names. Like {a:"accept", b:"backtrace", c:"class", d:"debug"}.
  7. * @return {object} Short names and values. Like {a:"xml", b:true, c:"new", d:true}
  8. */
  9. get: function(args, optNames) {
  10. var opt = {"_": []}; // the unnamed option allows multiple values
  11. for (var i = 0; i < args.length; i++) {
  12. var arg = new String(args[i]);
  13. var name;
  14. var value;
  15. if (arg.charAt(0) == "-") {
  16. if (arg.charAt(1) == "-") { // it's a longname like --foo
  17. arg = arg.substring(2);
  18. var m = arg.split("=");
  19. name = m.shift();
  20. value = m.shift();
  21. if (typeof value == "undefined") value = true;
  22. for (var n in optNames) { // convert it to a shortname
  23. if (name == optNames[n]) {
  24. name = n;
  25. }
  26. }
  27. }
  28. else { // it's a shortname like -f
  29. arg = arg.substring(1);
  30. var m = arg.split("=");
  31. name = m.shift();
  32. value = m.shift();
  33. if (typeof value == "undefined") value = true;
  34. for (var n in optNames) { // find the matching key
  35. if (name == n || name+'[]' == n) {
  36. name = n;
  37. break;
  38. }
  39. }
  40. }
  41. if (name.match(/(.+)\[\]$/)) { // it's an array type like n[]
  42. name = RegExp.$1;
  43. if (!opt[name]) opt[name] = [];
  44. }
  45. if (opt[name] && opt[name].push) {
  46. opt[name].push(value);
  47. }
  48. else {
  49. opt[name] = value;
  50. }
  51. }
  52. else { // not associated with any optname
  53. opt._.push(args[i]);
  54. }
  55. }
  56. return opt;
  57. }
  58. }
  59. /*t:
  60. plan(11, "Testing Opt.");
  61. is(
  62. typeof Opt,
  63. "object",
  64. "Opt is an object."
  65. );
  66. is(
  67. typeof Opt.get,
  68. "function",
  69. "Opt.get is a function."
  70. );
  71. var optNames = {a:"accept", b:"backtrace", c:"class", d:"debug", "e[]":"exceptions"};
  72. var t_options = Opt.get(["-a=xml", "-b", "--class=new", "--debug", "-e=one", "-e=two", "foo", "bar"], optNames);
  73. is(
  74. t_options.a,
  75. "xml",
  76. "an option defined with a short name can be accessed by its short name."
  77. );
  78. is(
  79. t_options.b,
  80. true,
  81. "an option defined with a short name and no value are true."
  82. );
  83. is(
  84. t_options.c,
  85. "new",
  86. "an option defined with a long name can be accessed by its short name."
  87. );
  88. is(
  89. t_options.d,
  90. true,
  91. "an option defined with a long name and no value are true."
  92. );
  93. is(
  94. typeof t_options.e,
  95. "object",
  96. "an option that can accept multiple values is defined."
  97. );
  98. is(
  99. t_options.e.length,
  100. 2,
  101. "an option that can accept multiple values can have more than one value."
  102. );
  103. is(
  104. t_options.e[1],
  105. "two",
  106. "an option that can accept multiple values can be accessed as an array."
  107. );
  108. is(
  109. typeof t_options._,
  110. "object",
  111. "the property '_' is defined for unnamed options."
  112. );
  113. is(
  114. t_options._[0],
  115. "foo",
  116. "the property '_' can be accessed as an array."
  117. );
  118. */