index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. module.exports = function (args, opts) {
  2. if (!opts) opts = {};
  3. var flags = { bools : {}, strings : {}, unknownFn: null };
  4. if (typeof opts['unknown'] === 'function') {
  5. flags.unknownFn = opts['unknown'];
  6. }
  7. if (typeof opts['boolean'] === 'boolean' && opts['boolean']) {
  8. flags.allBools = true;
  9. } else {
  10. [].concat(opts['boolean']).filter(Boolean).forEach(function (key) {
  11. flags.bools[key] = true;
  12. });
  13. }
  14. var aliases = {};
  15. Object.keys(opts.alias || {}).forEach(function (key) {
  16. aliases[key] = [].concat(opts.alias[key]);
  17. aliases[key].forEach(function (x) {
  18. aliases[x] = [key].concat(aliases[key].filter(function (y) {
  19. return x !== y;
  20. }));
  21. });
  22. });
  23. [].concat(opts.string).filter(Boolean).forEach(function (key) {
  24. flags.strings[key] = true;
  25. if (aliases[key]) {
  26. flags.strings[aliases[key]] = true;
  27. }
  28. });
  29. var defaults = opts['default'] || {};
  30. var argv = { _ : [] };
  31. Object.keys(flags.bools).forEach(function (key) {
  32. setArg(key, defaults[key] === undefined ? false : defaults[key]);
  33. });
  34. var notFlags = [];
  35. if (args.indexOf('--') !== -1) {
  36. notFlags = args.slice(args.indexOf('--')+1);
  37. args = args.slice(0, args.indexOf('--'));
  38. }
  39. function argDefined(key, arg) {
  40. return (flags.allBools && /^--[^=]+$/.test(arg)) ||
  41. flags.strings[key] || flags.bools[key] || aliases[key];
  42. }
  43. function setArg (key, val, arg) {
  44. if (arg && flags.unknownFn && !argDefined(key, arg)) {
  45. if (flags.unknownFn(arg) === false) return;
  46. }
  47. var value = !flags.strings[key] && isNumber(val)
  48. ? Number(val) : val
  49. ;
  50. setKey(argv, key.split('.'), value);
  51. (aliases[key] || []).forEach(function (x) {
  52. setKey(argv, x.split('.'), value);
  53. });
  54. }
  55. function setKey (obj, keys, value) {
  56. var o = obj;
  57. for (var i = 0; i < keys.length-1; i++) {
  58. var key = keys[i];
  59. if (isConstructorOrProto(o, key)) return;
  60. if (o[key] === undefined) o[key] = {};
  61. if (o[key] === Object.prototype || o[key] === Number.prototype
  62. || o[key] === String.prototype) o[key] = {};
  63. if (o[key] === Array.prototype) o[key] = [];
  64. o = o[key];
  65. }
  66. var key = keys[keys.length - 1];
  67. if (isConstructorOrProto(o, key)) return;
  68. if (o === Object.prototype || o === Number.prototype
  69. || o === String.prototype) o = {};
  70. if (o === Array.prototype) o = [];
  71. if (o[key] === undefined || flags.bools[key] || typeof o[key] === 'boolean') {
  72. o[key] = value;
  73. }
  74. else if (Array.isArray(o[key])) {
  75. o[key].push(value);
  76. }
  77. else {
  78. o[key] = [ o[key], value ];
  79. }
  80. }
  81. function aliasIsBoolean(key) {
  82. return aliases[key].some(function (x) {
  83. return flags.bools[x];
  84. });
  85. }
  86. for (var i = 0; i < args.length; i++) {
  87. var arg = args[i];
  88. if (/^--.+=/.test(arg)) {
  89. // Using [\s\S] instead of . because js doesn't support the
  90. // 'dotall' regex modifier. See:
  91. // http://stackoverflow.com/a/1068308/13216
  92. var m = arg.match(/^--([^=]+)=([\s\S]*)$/);
  93. var key = m[1];
  94. var value = m[2];
  95. if (flags.bools[key]) {
  96. value = value !== 'false';
  97. }
  98. setArg(key, value, arg);
  99. }
  100. else if (/^--no-.+/.test(arg)) {
  101. var key = arg.match(/^--no-(.+)/)[1];
  102. setArg(key, false, arg);
  103. }
  104. else if (/^--.+/.test(arg)) {
  105. var key = arg.match(/^--(.+)/)[1];
  106. var next = args[i + 1];
  107. if (next !== undefined && !/^-/.test(next)
  108. && !flags.bools[key]
  109. && !flags.allBools
  110. && (aliases[key] ? !aliasIsBoolean(key) : true)) {
  111. setArg(key, next, arg);
  112. i++;
  113. }
  114. else if (/^(true|false)$/.test(next)) {
  115. setArg(key, next === 'true', arg);
  116. i++;
  117. }
  118. else {
  119. setArg(key, flags.strings[key] ? '' : true, arg);
  120. }
  121. }
  122. else if (/^-[^-]+/.test(arg)) {
  123. var letters = arg.slice(1,-1).split('');
  124. var broken = false;
  125. for (var j = 0; j < letters.length; j++) {
  126. var next = arg.slice(j+2);
  127. if (next === '-') {
  128. setArg(letters[j], next, arg)
  129. continue;
  130. }
  131. if (/[A-Za-z]/.test(letters[j]) && /=/.test(next)) {
  132. setArg(letters[j], next.split('=')[1], arg);
  133. broken = true;
  134. break;
  135. }
  136. if (/[A-Za-z]/.test(letters[j])
  137. && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
  138. setArg(letters[j], next, arg);
  139. broken = true;
  140. break;
  141. }
  142. if (letters[j+1] && letters[j+1].match(/\W/)) {
  143. setArg(letters[j], arg.slice(j+2), arg);
  144. broken = true;
  145. break;
  146. }
  147. else {
  148. setArg(letters[j], flags.strings[letters[j]] ? '' : true, arg);
  149. }
  150. }
  151. var key = arg.slice(-1)[0];
  152. if (!broken && key !== '-') {
  153. if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
  154. && !flags.bools[key]
  155. && (aliases[key] ? !aliasIsBoolean(key) : true)) {
  156. setArg(key, args[i+1], arg);
  157. i++;
  158. }
  159. else if (args[i+1] && /^(true|false)$/.test(args[i+1])) {
  160. setArg(key, args[i+1] === 'true', arg);
  161. i++;
  162. }
  163. else {
  164. setArg(key, flags.strings[key] ? '' : true, arg);
  165. }
  166. }
  167. }
  168. else {
  169. if (!flags.unknownFn || flags.unknownFn(arg) !== false) {
  170. argv._.push(
  171. flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)
  172. );
  173. }
  174. if (opts.stopEarly) {
  175. argv._.push.apply(argv._, args.slice(i + 1));
  176. break;
  177. }
  178. }
  179. }
  180. Object.keys(defaults).forEach(function (key) {
  181. if (!hasKey(argv, key.split('.'))) {
  182. setKey(argv, key.split('.'), defaults[key]);
  183. (aliases[key] || []).forEach(function (x) {
  184. setKey(argv, x.split('.'), defaults[key]);
  185. });
  186. }
  187. });
  188. if (opts['--']) {
  189. argv['--'] = new Array();
  190. notFlags.forEach(function(key) {
  191. argv['--'].push(key);
  192. });
  193. }
  194. else {
  195. notFlags.forEach(function(key) {
  196. argv._.push(key);
  197. });
  198. }
  199. return argv;
  200. };
  201. function hasKey (obj, keys) {
  202. var o = obj;
  203. keys.slice(0,-1).forEach(function (key) {
  204. o = (o[key] || {});
  205. });
  206. var key = keys[keys.length - 1];
  207. return key in o;
  208. }
  209. function isNumber (x) {
  210. if (typeof x === 'number') return true;
  211. if (/^0x[0-9a-f]+$/i.test(x)) return true;
  212. return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
  213. }
  214. function isConstructorOrProto (obj, key) {
  215. return key === 'constructor' && typeof obj[key] === 'function' || key === '__proto__';
  216. }