index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. keys.slice(0,-1).forEach(function (key) {
  58. if (o[key] === undefined) o[key] = {};
  59. o = o[key];
  60. });
  61. var key = keys[keys.length - 1];
  62. if (o[key] === undefined || flags.bools[key] || typeof o[key] === 'boolean') {
  63. o[key] = value;
  64. }
  65. else if (Array.isArray(o[key])) {
  66. o[key].push(value);
  67. }
  68. else {
  69. o[key] = [ o[key], value ];
  70. }
  71. }
  72. function aliasIsBoolean(key) {
  73. return aliases[key].some(function (x) {
  74. return flags.bools[x];
  75. });
  76. }
  77. for (var i = 0; i < args.length; i++) {
  78. var arg = args[i];
  79. if (/^--.+=/.test(arg)) {
  80. // Using [\s\S] instead of . because js doesn't support the
  81. // 'dotall' regex modifier. See:
  82. // http://stackoverflow.com/a/1068308/13216
  83. var m = arg.match(/^--([^=]+)=([\s\S]*)$/);
  84. var key = m[1];
  85. var value = m[2];
  86. if (flags.bools[key]) {
  87. value = value !== 'false';
  88. }
  89. setArg(key, value, arg);
  90. }
  91. else if (/^--no-.+/.test(arg)) {
  92. var key = arg.match(/^--no-(.+)/)[1];
  93. setArg(key, false, arg);
  94. }
  95. else if (/^--.+/.test(arg)) {
  96. var key = arg.match(/^--(.+)/)[1];
  97. var next = args[i + 1];
  98. if (next !== undefined && !/^-/.test(next)
  99. && !flags.bools[key]
  100. && !flags.allBools
  101. && (aliases[key] ? !aliasIsBoolean(key) : true)) {
  102. setArg(key, next, arg);
  103. i++;
  104. }
  105. else if (/^(true|false)$/.test(next)) {
  106. setArg(key, next === 'true', arg);
  107. i++;
  108. }
  109. else {
  110. setArg(key, flags.strings[key] ? '' : true, arg);
  111. }
  112. }
  113. else if (/^-[^-]+/.test(arg)) {
  114. var letters = arg.slice(1,-1).split('');
  115. var broken = false;
  116. for (var j = 0; j < letters.length; j++) {
  117. var next = arg.slice(j+2);
  118. if (next === '-') {
  119. setArg(letters[j], next, arg)
  120. continue;
  121. }
  122. if (/[A-Za-z]/.test(letters[j])
  123. && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
  124. setArg(letters[j], next, arg);
  125. broken = true;
  126. break;
  127. }
  128. if (letters[j+1] && letters[j+1].match(/\W/)) {
  129. setArg(letters[j], arg.slice(j+2), arg);
  130. broken = true;
  131. break;
  132. }
  133. else {
  134. setArg(letters[j], flags.strings[letters[j]] ? '' : true, arg);
  135. }
  136. }
  137. var key = arg.slice(-1)[0];
  138. if (!broken && key !== '-') {
  139. if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
  140. && !flags.bools[key]
  141. && (aliases[key] ? !aliasIsBoolean(key) : true)) {
  142. setArg(key, args[i+1], arg);
  143. i++;
  144. }
  145. else if (args[i+1] && /true|false/.test(args[i+1])) {
  146. setArg(key, args[i+1] === 'true', arg);
  147. i++;
  148. }
  149. else {
  150. setArg(key, flags.strings[key] ? '' : true, arg);
  151. }
  152. }
  153. }
  154. else {
  155. if (!flags.unknownFn || flags.unknownFn(arg) !== false) {
  156. argv._.push(
  157. flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)
  158. );
  159. }
  160. if (opts.stopEarly) {
  161. argv._.push.apply(argv._, args.slice(i + 1));
  162. break;
  163. }
  164. }
  165. }
  166. Object.keys(defaults).forEach(function (key) {
  167. if (!hasKey(argv, key.split('.'))) {
  168. setKey(argv, key.split('.'), defaults[key]);
  169. (aliases[key] || []).forEach(function (x) {
  170. setKey(argv, x.split('.'), defaults[key]);
  171. });
  172. }
  173. });
  174. if (opts['--']) {
  175. argv['--'] = new Array();
  176. notFlags.forEach(function(key) {
  177. argv['--'].push(key);
  178. });
  179. }
  180. else {
  181. notFlags.forEach(function(key) {
  182. argv._.push(key);
  183. });
  184. }
  185. return argv;
  186. };
  187. function hasKey (obj, keys) {
  188. var o = obj;
  189. keys.slice(0,-1).forEach(function (key) {
  190. o = (o[key] || {});
  191. });
  192. var key = keys[keys.length - 1];
  193. return key in o;
  194. }
  195. function isNumber (x) {
  196. if (typeof x === 'number') return true;
  197. if (/^0x[0-9a-f]+$/i.test(x)) return true;
  198. return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
  199. }