unknown.js 2.5 KB

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