parseargs.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Jake JavaScript build tool
  3. * Copyright 2112 Matthew Eernisse ([email protected])
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. var parseargs = {};
  19. /**
  20. * @constructor
  21. * Parses a list of command-line args into a key/value object of
  22. * options and an array of positional commands.
  23. * @ param {Array} opts A list of options in the following format:
  24. * [{full: 'foo', abbr: 'f'}, {full: 'bar', abbr: 'b'}]]
  25. */
  26. parseargs.Parser = function (opts) {
  27. // A key/value object of matching options parsed out of the args
  28. this.opts = {};
  29. this.taskNames = null;
  30. this.envVars = null;
  31. // Data structures used for parsing
  32. this.reg = [];
  33. this.shortOpts = {};
  34. this.longOpts = {};
  35. var item;
  36. for (var i = 0, ii = opts.length; i < ii; i++) {
  37. item = opts[i];
  38. this.shortOpts[item.abbr] = item;
  39. this.longOpts[item.full] = item;
  40. }
  41. this.reg = opts;
  42. };
  43. parseargs.Parser.prototype = new function () {
  44. var _trueOrNextVal = function (argParts, args) {
  45. if (argParts[1]) {
  46. return argParts[1];
  47. }
  48. else {
  49. return (!args[0] || (args[0].indexOf('-') === 0)) ?
  50. true : args.shift();
  51. }
  52. };
  53. /**
  54. * Parses an array of arguments into options and positional commands
  55. * @param {Array} args The command-line args to parse
  56. */
  57. this.parse = function (args) {
  58. var cmds = []
  59. , cmd
  60. , envVars = {}
  61. , opts = {}
  62. , arg
  63. , argItem
  64. , argParts
  65. , cmdItems
  66. , taskNames = []
  67. , preempt;
  68. while (args.length) {
  69. arg = args.shift();
  70. if (arg.indexOf('-') === 0) {
  71. arg = arg.replace(/^--/, '').replace(/^-/, '');
  72. argParts = arg.split('=');
  73. argItem = this.longOpts[argParts[0]] || this.shortOpts[argParts[0]];
  74. if (argItem) {
  75. // First-encountered preemptive opt takes precedence -- no further opts
  76. // or possibility of ambiguity, so just look for a value, or set to
  77. // true and then bail
  78. if (argItem.preempts) {
  79. opts[argItem.full] = _trueOrNextVal(argParts, args);
  80. preempt = true;
  81. break;
  82. }
  83. // If the opt requires a value, see if we can get a value from the
  84. // next arg, or infer true from no-arg -- if it's followed by another
  85. // opt, throw an error
  86. if (argItem.expectValue) {
  87. opts[argItem.full] = _trueOrNextVal(argParts, args);
  88. if (!opts[argItem.full]) {
  89. throw new Error(argItem.full + ' option expects a value.');
  90. }
  91. }
  92. else {
  93. opts[argItem.full] = true;
  94. }
  95. }
  96. }
  97. else {
  98. cmds.unshift(arg);
  99. }
  100. }
  101. if (!preempt) {
  102. // Parse out any env-vars and task-name
  103. while (!!(cmd = cmds.pop())) {
  104. cmdItems = cmd.split('=');
  105. if (cmdItems.length > 1) {
  106. envVars[cmdItems[0]] = cmdItems[1];
  107. }
  108. else {
  109. taskNames.push(cmd);
  110. }
  111. }
  112. }
  113. return {
  114. opts: opts
  115. , envVars: envVars
  116. , taskNames: taskNames
  117. };
  118. };
  119. };
  120. module.exports = parseargs;