validation.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // validation-type-stuff, missing params,
  2. // bad implications, custom checks.
  3. module.exports = function (yargs, usage) {
  4. var self = {};
  5. // validate appropriate # of non-option
  6. // arguments were provided, i.e., '_'.
  7. self.nonOptionCount = function(argv) {
  8. var demanded = yargs.getDemanded();
  9. if (demanded._ && argv._.length < demanded._.count) {
  10. if (demanded._.msg !== undefined) {
  11. usage.fail(demanded._.msg);
  12. } else {
  13. usage.fail('Not enough non-option arguments: got '
  14. + argv._.length + ', need at least ' + demanded._.count
  15. );
  16. }
  17. }
  18. };
  19. // make sure that any args that require an
  20. // value (--foo=bar), have a value.
  21. self.missingArgumentValue = function(argv) {
  22. var options = yargs.getOptions();
  23. if (options.requiresArg.length > 0) {
  24. var missingRequiredArgs = [];
  25. options.requiresArg.forEach(function(key) {
  26. var value = argv[key];
  27. // parser sets --foo value to true / --no-foo to false
  28. if (value === true || value === false) {
  29. missingRequiredArgs.push(key);
  30. }
  31. });
  32. if (missingRequiredArgs.length == 1) {
  33. usage.fail("Missing argument value: " + missingRequiredArgs[0]);
  34. }
  35. else if (missingRequiredArgs.length > 1) {
  36. var message = "Missing argument values: " + missingRequiredArgs.join(", ");
  37. usage.fail(message);
  38. }
  39. }
  40. };
  41. // make sure all the required arguments are present.
  42. self.requiredArguments = function(argv) {
  43. var demanded = yargs.getDemanded(),
  44. missing = null;
  45. Object.keys(demanded).forEach(function (key) {
  46. if (!argv.hasOwnProperty(key)) {
  47. missing = missing || {};
  48. missing[key] = demanded[key];
  49. }
  50. });
  51. if (missing) {
  52. var customMsgs = [];
  53. Object.keys(missing).forEach(function(key) {
  54. var msg = missing[key].msg;
  55. if (msg && customMsgs.indexOf(msg) < 0) {
  56. customMsgs.push(msg);
  57. }
  58. });
  59. var customMsg = customMsgs.length ? '\n' + customMsgs.join('\n') : '';
  60. usage.fail('Missing required arguments: ' + Object.keys(missing).join(', ') + customMsg);
  61. }
  62. };
  63. // check for unknown arguments (strict-mode).
  64. self.unknownArguments = function(argv, aliases) {
  65. var descriptions = usage.getDescriptions(),
  66. demanded = yargs.getDemanded(),
  67. unknown = [],
  68. aliasLookup = {};
  69. Object.keys(aliases).forEach(function (key) {
  70. aliases[key].forEach(function (alias) {
  71. aliasLookup[alias] = key;
  72. });
  73. });
  74. Object.keys(argv).forEach(function (key) {
  75. if (key !== "$0" && key !== "_" &&
  76. !descriptions.hasOwnProperty(key) &&
  77. !demanded.hasOwnProperty(key) &&
  78. !aliasLookup.hasOwnProperty(key)) {
  79. unknown.push(key);
  80. }
  81. });
  82. if (unknown.length == 1) {
  83. usage.fail("Unknown argument: " + unknown[0]);
  84. }
  85. else if (unknown.length > 1) {
  86. usage.fail("Unknown arguments: " + unknown.join(", "));
  87. }
  88. };
  89. // custom checks, added using the `check` option on yargs.
  90. var checks = [];
  91. self.check = function (f) {
  92. checks.push(f);
  93. };
  94. self.customChecks = function(argv, aliases) {
  95. checks.forEach(function (f) {
  96. try {
  97. var result = f(argv, aliases);
  98. if (!result) {
  99. usage.fail('Argument check failed: ' + f.toString());
  100. } else if (typeof result === 'string') {
  101. usage.fail(result);
  102. }
  103. }
  104. catch (err) {
  105. usage.fail(err)
  106. }
  107. });
  108. };
  109. // check implications, argument foo implies => argument bar.
  110. var implied = {};
  111. self.implies = function (key, value) {
  112. if (typeof key === 'object') {
  113. Object.keys(key).forEach(function (k) {
  114. self.implies(k, key[k]);
  115. });
  116. } else {
  117. implied[key] = value;
  118. }
  119. };
  120. self.getImplied = function() {
  121. return implied;
  122. }
  123. self.implications = function(argv) {
  124. var implyFail = [];
  125. Object.keys(implied).forEach(function (key) {
  126. var num, origKey = key, value = implied[key];
  127. // convert string '1' to number 1
  128. var num = Number(key);
  129. key = isNaN(num) ? key : num;
  130. if (typeof key === 'number') {
  131. // check length of argv._
  132. key = argv._.length >= key;
  133. } else if (key.match(/^--no-.+/)) {
  134. // check if key doesn't exist
  135. key = key.match(/^--no-(.+)/)[1];
  136. key = !argv[key];
  137. } else {
  138. // check if key exists
  139. key = argv[key];
  140. }
  141. num = Number(value);
  142. value = isNaN(num) ? value : num;
  143. if (typeof value === 'number') {
  144. value = argv._.length >= value;
  145. } else if (value.match(/^--no-.+/)) {
  146. value = value.match(/^--no-(.+)/)[1];
  147. value = !argv[value];
  148. } else {
  149. value = argv[value];
  150. }
  151. if (key && !value) {
  152. implyFail.push(origKey);
  153. }
  154. });
  155. if (implyFail.length) {
  156. var msg = 'Implications failed:\n';
  157. implyFail.forEach(function (key) {
  158. msg += (' ' + key + ' -> ' + implied[key]);
  159. });
  160. usage.fail(msg);
  161. }
  162. }
  163. return self;
  164. }