unknown.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. var parse = require('../');
  3. var test = require('tape');
  4. test('boolean and alias is not unknown', function (t) {
  5. var unknown = [];
  6. function unknownFn(arg) {
  7. unknown.push(arg);
  8. return false;
  9. }
  10. var aliased = ['-h', 'true', '--derp', 'true'];
  11. var regular = ['--herp', 'true', '-d', 'true'];
  12. var opts = {
  13. alias: { h: 'herp' },
  14. boolean: 'h',
  15. unknown: unknownFn,
  16. };
  17. parse(aliased, opts);
  18. parse(regular, opts);
  19. t.same(unknown, ['--derp', '-d']);
  20. t.end();
  21. });
  22. test('flag boolean true any double hyphen argument is not unknown', function (t) {
  23. var unknown = [];
  24. function unknownFn(arg) {
  25. unknown.push(arg);
  26. return false;
  27. }
  28. var argv = parse(['--honk', '--tacos=good', 'cow', '-p', '55'], {
  29. boolean: true,
  30. unknown: unknownFn,
  31. });
  32. t.same(unknown, ['--tacos=good', 'cow', '-p']);
  33. t.same(argv, {
  34. honk: true,
  35. _: [],
  36. });
  37. t.end();
  38. });
  39. test('string and alias is not unknown', function (t) {
  40. var unknown = [];
  41. function unknownFn(arg) {
  42. unknown.push(arg);
  43. return false;
  44. }
  45. var aliased = ['-h', 'hello', '--derp', 'goodbye'];
  46. var regular = ['--herp', 'hello', '-d', 'moon'];
  47. var opts = {
  48. alias: { h: 'herp' },
  49. string: 'h',
  50. unknown: unknownFn,
  51. };
  52. parse(aliased, opts);
  53. parse(regular, opts);
  54. t.same(unknown, ['--derp', '-d']);
  55. t.end();
  56. });
  57. test('default and alias is not unknown', function (t) {
  58. var unknown = [];
  59. function unknownFn(arg) {
  60. unknown.push(arg);
  61. return false;
  62. }
  63. var aliased = ['-h', 'hello'];
  64. var regular = ['--herp', 'hello'];
  65. var opts = {
  66. default: { h: 'bar' },
  67. alias: { h: 'herp' },
  68. unknown: unknownFn,
  69. };
  70. parse(aliased, opts);
  71. parse(regular, opts);
  72. t.same(unknown, []);
  73. t.end();
  74. unknownFn(); // exercise fn for 100% coverage
  75. });
  76. test('value following -- is not unknown', function (t) {
  77. var unknown = [];
  78. function unknownFn(arg) {
  79. unknown.push(arg);
  80. return false;
  81. }
  82. var aliased = ['--bad', '--', 'good', 'arg'];
  83. var opts = {
  84. '--': true,
  85. unknown: unknownFn,
  86. };
  87. var argv = parse(aliased, opts);
  88. t.same(unknown, ['--bad']);
  89. t.same(argv, {
  90. '--': ['good', 'arg'],
  91. _: [],
  92. });
  93. t.end();
  94. });