index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. module.exports = function (args, opts) {
  2. if (!opts) opts = {};
  3. var flags = { bools : {}, strings : {} };
  4. [].concat(opts['boolean']).filter(Boolean).forEach(function (key) {
  5. flags.bools[key] = true;
  6. });
  7. var aliases = {};
  8. Object.keys(opts.alias || {}).forEach(function (key) {
  9. aliases[key] = [].concat(opts.alias[key]);
  10. aliases[key].forEach(function (x) {
  11. aliases[x] = [key].concat(aliases[key].filter(function (y) {
  12. return x !== y;
  13. }));
  14. });
  15. });
  16. [].concat(opts.string).filter(Boolean).forEach(function (key) {
  17. flags.strings[key] = true;
  18. if (aliases[key]) {
  19. flags.strings[aliases[key]] = true;
  20. }
  21. });
  22. var defaults = opts['default'] || {};
  23. var argv = { _ : [] };
  24. Object.keys(flags.bools).forEach(function (key) {
  25. setArg(key, defaults[key] === undefined ? false : defaults[key]);
  26. });
  27. var notFlags = [];
  28. if (args.indexOf('--') !== -1) {
  29. notFlags = args.slice(args.indexOf('--')+1);
  30. args = args.slice(0, args.indexOf('--'));
  31. }
  32. function setArg (key, val) {
  33. var value = !flags.strings[key] && isNumber(val)
  34. ? Number(val) : val
  35. ;
  36. setKey(argv, key.split('.'), value);
  37. (aliases[key] || []).forEach(function (x) {
  38. setKey(argv, x.split('.'), value);
  39. });
  40. }
  41. for (var i = 0; i < args.length; i++) {
  42. var arg = args[i];
  43. if (/^--.+=/.test(arg)) {
  44. // Using [\s\S] instead of . because js doesn't support the
  45. // 'dotall' regex modifier. See:
  46. // http://stackoverflow.com/a/1068308/13216
  47. var m = arg.match(/^--([^=]+)=([\s\S]*)$/);
  48. setArg(m[1], m[2]);
  49. }
  50. else if (/^--no-.+/.test(arg)) {
  51. var key = arg.match(/^--no-(.+)/)[1];
  52. setArg(key, false);
  53. }
  54. else if (/^--.+/.test(arg)) {
  55. var key = arg.match(/^--(.+)/)[1];
  56. var next = args[i + 1];
  57. if (next !== undefined && !/^-/.test(next)
  58. && !flags.bools[key]
  59. && (aliases[key] ? !flags.bools[aliases[key]] : true)) {
  60. setArg(key, next);
  61. i++;
  62. }
  63. else if (/^(true|false)$/.test(next)) {
  64. setArg(key, next === 'true');
  65. i++;
  66. }
  67. else {
  68. setArg(key, flags.strings[key] ? '' : true);
  69. }
  70. }
  71. else if (/^-[^-]+/.test(arg)) {
  72. var letters = arg.slice(1,-1).split('');
  73. var broken = false;
  74. for (var j = 0; j < letters.length; j++) {
  75. var next = arg.slice(j+2);
  76. if (next === '-') {
  77. setArg(letters[j], next)
  78. continue;
  79. }
  80. if (/[A-Za-z]/.test(letters[j])
  81. && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
  82. setArg(letters[j], next);
  83. broken = true;
  84. break;
  85. }
  86. if (letters[j+1] && letters[j+1].match(/\W/)) {
  87. setArg(letters[j], arg.slice(j+2));
  88. broken = true;
  89. break;
  90. }
  91. else {
  92. setArg(letters[j], flags.strings[letters[j]] ? '' : true);
  93. }
  94. }
  95. var key = arg.slice(-1)[0];
  96. if (!broken && key !== '-') {
  97. if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
  98. && !flags.bools[key]
  99. && (aliases[key] ? !flags.bools[aliases[key]] : true)) {
  100. setArg(key, args[i+1]);
  101. i++;
  102. }
  103. else if (args[i+1] && /true|false/.test(args[i+1])) {
  104. setArg(key, args[i+1] === 'true');
  105. i++;
  106. }
  107. else {
  108. setArg(key, flags.strings[key] ? '' : true);
  109. }
  110. }
  111. }
  112. else {
  113. argv._.push(
  114. flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)
  115. );
  116. }
  117. }
  118. Object.keys(defaults).forEach(function (key) {
  119. if (!hasKey(argv, key.split('.'))) {
  120. setKey(argv, key.split('.'), defaults[key]);
  121. (aliases[key] || []).forEach(function (x) {
  122. setKey(argv, x.split('.'), defaults[key]);
  123. });
  124. }
  125. });
  126. notFlags.forEach(function(key) {
  127. argv._.push(key);
  128. });
  129. return argv;
  130. };
  131. function hasKey (obj, keys) {
  132. var o = obj;
  133. keys.slice(0,-1).forEach(function (key) {
  134. o = (o[key] || {});
  135. });
  136. var key = keys[keys.length - 1];
  137. return key in o;
  138. }
  139. function setKey (obj, keys, value) {
  140. var o = obj;
  141. keys.slice(0,-1).forEach(function (key) {
  142. if (o[key] === undefined) o[key] = {};
  143. o = o[key];
  144. });
  145. var key = keys[keys.length - 1];
  146. if (o[key] === undefined || typeof o[key] === 'boolean') {
  147. o[key] = value;
  148. }
  149. else if (Array.isArray(o[key])) {
  150. o[key].push(value);
  151. }
  152. else {
  153. o[key] = [ o[key], value ];
  154. }
  155. }
  156. function isNumber (x) {
  157. if (typeof x === 'number') return true;
  158. if (/^0x[0-9a-f]+$/i.test(x)) return true;
  159. return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
  160. }