parseargs.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Jake JavaScript build tool
  3. * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
  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. let parseargs = {};
  19. let isOpt = function (arg) { return arg.indexOf('-') === 0; };
  20. let removeOptPrefix = function (opt) { return opt.replace(/^--/, '').replace(/^-/, ''); };
  21. /**
  22. * @constructor
  23. * Parses a list of command-line args into a key/value object of
  24. * options and an array of positional commands.
  25. * @ param {Array} opts A list of options in the following format:
  26. * [{full: 'foo', abbr: 'f'}, {full: 'bar', abbr: 'b'}]]
  27. */
  28. parseargs.Parser = function (opts) {
  29. // A key/value object of matching options parsed out of the args
  30. this.opts = {};
  31. this.taskNames = null;
  32. this.envVars = null;
  33. // Data structures used for parsing
  34. this.reg = opts;
  35. this.shortOpts = {};
  36. this.longOpts = {};
  37. let self = this;
  38. [].forEach.call(opts, function (item) {
  39. self.shortOpts[item.abbr] = item;
  40. self.longOpts[item.full] = item;
  41. });
  42. };
  43. parseargs.Parser.prototype = new function () {
  44. let _trueOrNextVal = function (argParts, args) {
  45. if (argParts[1]) {
  46. return argParts[1];
  47. }
  48. else {
  49. return (!args[0] || isOpt(args[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. let cmds = [];
  59. let cmd;
  60. let envVars = {};
  61. let opts = {};
  62. let arg;
  63. let argItem;
  64. let argParts;
  65. let cmdItems;
  66. let taskNames = [];
  67. let preempt;
  68. while (args.length) {
  69. arg = args.shift();
  70. if (isOpt(arg)) {
  71. arg = removeOptPrefix(arg);
  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 || argItem.allowValue) {
  87. opts[argItem.full] = _trueOrNextVal(argParts, args);
  88. if (argItem.expectValue && !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;