555
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cliargs.js 6.7KB

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