optparse.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Generated by CoffeeScript 1.12.6
  2. (function() {
  3. var LONG_FLAG, MULTI_FLAG, OPTIONAL, OptionParser, SHORT_FLAG, buildRule, buildRules, normalizeArguments, repeat;
  4. repeat = require('./helpers').repeat;
  5. exports.OptionParser = OptionParser = (function() {
  6. function OptionParser(rules, banner) {
  7. this.banner = banner;
  8. this.rules = buildRules(rules);
  9. }
  10. OptionParser.prototype.parse = function(args) {
  11. var arg, i, isOption, j, k, len, len1, matchedRule, options, originalArgs, pos, ref, rule, seenNonOptionArg, skippingArgument, value;
  12. options = {
  13. "arguments": []
  14. };
  15. skippingArgument = false;
  16. originalArgs = args;
  17. args = normalizeArguments(args);
  18. for (i = j = 0, len = args.length; j < len; i = ++j) {
  19. arg = args[i];
  20. if (skippingArgument) {
  21. skippingArgument = false;
  22. continue;
  23. }
  24. if (arg === '--') {
  25. pos = originalArgs.indexOf('--');
  26. options["arguments"] = options["arguments"].concat(originalArgs.slice(pos + 1));
  27. break;
  28. }
  29. isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
  30. seenNonOptionArg = options["arguments"].length > 0;
  31. if (!seenNonOptionArg) {
  32. matchedRule = false;
  33. ref = this.rules;
  34. for (k = 0, len1 = ref.length; k < len1; k++) {
  35. rule = ref[k];
  36. if (rule.shortFlag === arg || rule.longFlag === arg) {
  37. value = true;
  38. if (rule.hasArgument) {
  39. skippingArgument = true;
  40. value = args[i + 1];
  41. }
  42. options[rule.name] = rule.isList ? (options[rule.name] || []).concat(value) : value;
  43. matchedRule = true;
  44. break;
  45. }
  46. }
  47. if (isOption && !matchedRule) {
  48. throw new Error("unrecognized option: " + arg);
  49. }
  50. }
  51. if (seenNonOptionArg || !isOption) {
  52. options["arguments"].push(arg);
  53. }
  54. }
  55. return options;
  56. };
  57. OptionParser.prototype.help = function() {
  58. var j, len, letPart, lines, ref, rule, spaces;
  59. lines = [];
  60. if (this.banner) {
  61. lines.unshift(this.banner + "\n");
  62. }
  63. ref = this.rules;
  64. for (j = 0, len = ref.length; j < len; j++) {
  65. rule = ref[j];
  66. spaces = 15 - rule.longFlag.length;
  67. spaces = spaces > 0 ? repeat(' ', spaces) : '';
  68. letPart = rule.shortFlag ? rule.shortFlag + ', ' : ' ';
  69. lines.push(' ' + letPart + rule.longFlag + spaces + rule.description);
  70. }
  71. return "\n" + (lines.join('\n')) + "\n";
  72. };
  73. return OptionParser;
  74. })();
  75. LONG_FLAG = /^(--\w[\w\-]*)/;
  76. SHORT_FLAG = /^(-\w)$/;
  77. MULTI_FLAG = /^-(\w{2,})/;
  78. OPTIONAL = /\[(\w+(\*?))\]/;
  79. buildRules = function(rules) {
  80. var j, len, results, tuple;
  81. results = [];
  82. for (j = 0, len = rules.length; j < len; j++) {
  83. tuple = rules[j];
  84. if (tuple.length < 3) {
  85. tuple.unshift(null);
  86. }
  87. results.push(buildRule.apply(null, tuple));
  88. }
  89. return results;
  90. };
  91. buildRule = function(shortFlag, longFlag, description, options) {
  92. var match;
  93. if (options == null) {
  94. options = {};
  95. }
  96. match = longFlag.match(OPTIONAL);
  97. longFlag = longFlag.match(LONG_FLAG)[1];
  98. return {
  99. name: longFlag.substr(2),
  100. shortFlag: shortFlag,
  101. longFlag: longFlag,
  102. description: description,
  103. hasArgument: !!(match && match[1]),
  104. isList: !!(match && match[2])
  105. };
  106. };
  107. normalizeArguments = function(args) {
  108. var arg, j, k, l, len, len1, match, ref, result;
  109. args = args.slice(0);
  110. result = [];
  111. for (j = 0, len = args.length; j < len; j++) {
  112. arg = args[j];
  113. if (match = arg.match(MULTI_FLAG)) {
  114. ref = match[1].split('');
  115. for (k = 0, len1 = ref.length; k < len1; k++) {
  116. l = ref[k];
  117. result.push('-' + l);
  118. }
  119. } else {
  120. result.push(arg);
  121. }
  122. }
  123. return result;
  124. };
  125. }).call(this);