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.

166 lines
3.9 KiB

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