bool.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. var parse = require('../');
  3. var test = require('tape');
  4. test('flag boolean default false', function (t) {
  5. var argv = parse(['moo'], {
  6. boolean: ['t', 'verbose'],
  7. default: { verbose: false, t: false },
  8. });
  9. t.deepEqual(argv, {
  10. verbose: false,
  11. t: false,
  12. _: ['moo'],
  13. });
  14. t.deepEqual(typeof argv.verbose, 'boolean');
  15. t.deepEqual(typeof argv.t, 'boolean');
  16. t.end();
  17. });
  18. test('boolean groups', function (t) {
  19. var argv = parse(['-x', '-z', 'one', 'two', 'three'], {
  20. boolean: ['x', 'y', 'z'],
  21. });
  22. t.deepEqual(argv, {
  23. x: true,
  24. y: false,
  25. z: true,
  26. _: ['one', 'two', 'three'],
  27. });
  28. t.deepEqual(typeof argv.x, 'boolean');
  29. t.deepEqual(typeof argv.y, 'boolean');
  30. t.deepEqual(typeof argv.z, 'boolean');
  31. t.end();
  32. });
  33. test('boolean and alias with chainable api', function (t) {
  34. var aliased = ['-h', 'derp'];
  35. var regular = ['--herp', 'derp'];
  36. var aliasedArgv = parse(aliased, {
  37. boolean: 'herp',
  38. alias: { h: 'herp' },
  39. });
  40. var propertyArgv = parse(regular, {
  41. boolean: 'herp',
  42. alias: { h: 'herp' },
  43. });
  44. var expected = {
  45. herp: true,
  46. h: true,
  47. _: ['derp'],
  48. };
  49. t.same(aliasedArgv, expected);
  50. t.same(propertyArgv, expected);
  51. t.end();
  52. });
  53. test('boolean and alias with options hash', function (t) {
  54. var aliased = ['-h', 'derp'];
  55. var regular = ['--herp', 'derp'];
  56. var opts = {
  57. alias: { h: 'herp' },
  58. boolean: 'herp',
  59. };
  60. var aliasedArgv = parse(aliased, opts);
  61. var propertyArgv = parse(regular, opts);
  62. var expected = {
  63. herp: true,
  64. h: true,
  65. _: ['derp'],
  66. };
  67. t.same(aliasedArgv, expected);
  68. t.same(propertyArgv, expected);
  69. t.end();
  70. });
  71. test('boolean and alias array with options hash', function (t) {
  72. var aliased = ['-h', 'derp'];
  73. var regular = ['--herp', 'derp'];
  74. var alt = ['--harp', 'derp'];
  75. var opts = {
  76. alias: { h: ['herp', 'harp'] },
  77. boolean: 'h',
  78. };
  79. var aliasedArgv = parse(aliased, opts);
  80. var propertyArgv = parse(regular, opts);
  81. var altPropertyArgv = parse(alt, opts);
  82. var expected = {
  83. harp: true,
  84. herp: true,
  85. h: true,
  86. _: ['derp'],
  87. };
  88. t.same(aliasedArgv, expected);
  89. t.same(propertyArgv, expected);
  90. t.same(altPropertyArgv, expected);
  91. t.end();
  92. });
  93. test('boolean and alias using explicit true', function (t) {
  94. var aliased = ['-h', 'true'];
  95. var regular = ['--herp', 'true'];
  96. var opts = {
  97. alias: { h: 'herp' },
  98. boolean: 'h',
  99. };
  100. var aliasedArgv = parse(aliased, opts);
  101. var propertyArgv = parse(regular, opts);
  102. var expected = {
  103. herp: true,
  104. h: true,
  105. _: [],
  106. };
  107. t.same(aliasedArgv, expected);
  108. t.same(propertyArgv, expected);
  109. t.end();
  110. });
  111. // regression, see https://github.com/substack/node-optimist/issues/71
  112. test('boolean and --x=true', function (t) {
  113. var parsed = parse(['--boool', '--other=true'], {
  114. boolean: 'boool',
  115. });
  116. t.same(parsed.boool, true);
  117. t.same(parsed.other, 'true');
  118. parsed = parse(['--boool', '--other=false'], {
  119. boolean: 'boool',
  120. });
  121. t.same(parsed.boool, true);
  122. t.same(parsed.other, 'false');
  123. t.end();
  124. });
  125. test('boolean --boool=true', function (t) {
  126. var parsed = parse(['--boool=true'], {
  127. default: {
  128. boool: false,
  129. },
  130. boolean: ['boool'],
  131. });
  132. t.same(parsed.boool, true);
  133. t.end();
  134. });
  135. test('boolean --boool=false', function (t) {
  136. var parsed = parse(['--boool=false'], {
  137. default: {
  138. boool: true,
  139. },
  140. boolean: ['boool'],
  141. });
  142. t.same(parsed.boool, false);
  143. t.end();
  144. });
  145. test('boolean using something similar to true', function (t) {
  146. var opts = { boolean: 'h' };
  147. var result = parse(['-h', 'true.txt'], opts);
  148. var expected = {
  149. h: true,
  150. _: ['true.txt'],
  151. };
  152. t.same(result, expected);
  153. t.end();
  154. });