Brak opisu

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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]) && /=/.test(next)) {
  123. setArg(letters[j], next.split('=')[1], arg);
  124. broken = true;
  125. break;
  126. }
  127. if (/[A-Za-z]/.test(letters[j])
  128. && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
  129. setArg(letters[j], next, arg);
  130. broken = true;
  131. break;
  132. }
  133. if (letters[j+1] && letters[j+1].match(/\W/)) {
  134. setArg(letters[j], arg.slice(j+2), arg);
  135. broken = true;
  136. break;
  137. }
  138. else {
  139. setArg(letters[j], flags.strings[letters[j]] ? '' : true, arg);
  140. }
  141. }
  142. var key = arg.slice(-1)[0];
  143. if (!broken && key !== '-') {
  144. if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1])
  145. && !flags.bools[key]
  146. && (aliases[key] ? !aliasIsBoolean(key) : true)) {
  147. setArg(key, args[i+1], arg);
  148. i++;
  149. }
  150. else if (args[i+1] && /true|false/.test(args[i+1])) {
  151. setArg(key, args[i+1] === 'true', arg);
  152. i++;
  153. }
  154. else {
  155. setArg(key, flags.strings[key] ? '' : true, arg);
  156. }
  157. }
  158. }
  159. else {
  160. if (!flags.unknownFn || flags.unknownFn(arg) !== false) {
  161. argv._.push(
  162. flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)
  163. );
  164. }
  165. if (opts.stopEarly) {
  166. argv._.push.apply(argv._, args.slice(i + 1));
  167. break;
  168. }
  169. }
  170. }
  171. Object.keys(defaults).forEach(function (key) {
  172. if (!hasKey(argv, key.split('.'))) {
  173. setKey(argv, key.split('.'), defaults[key]);
  174. (aliases[key] || []).forEach(function (x) {
  175. setKey(argv, x.split('.'), defaults[key]);
  176. });
  177. }
  178. });
  179. if (opts['--']) {
  180. argv['--'] = new Array();
  181. notFlags.forEach(function(key) {
  182. argv['--'].push(key);
  183. });
  184. }
  185. else {
  186. notFlags.forEach(function(key) {
  187. argv._.push(key);
  188. });
  189. }
  190. return argv;
  191. };
  192. function hasKey (obj, keys) {
  193. var o = obj;
  194. keys.slice(0,-1).forEach(function (key) {
  195. o = (o[key] || {});
  196. });
  197. var key = keys[keys.length - 1];
  198. return key in o;
  199. }
  200. function isNumber (x) {
  201. if (typeof x === 'number') return true;
  202. if (/^0x[0-9a-f]+$/i.test(x)) return true;
  203. return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
  204. }