No Description

optparse.js 4.0KB

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