bool.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 using explicit true', function (t) {
  74. var aliased = [ '-h', 'true' ];
  75. var regular = [ '--herp', 'true' ];
  76. var opts = {
  77. alias: { h: 'herp' },
  78. boolean: 'h'
  79. };
  80. var aliasedArgv = parse(aliased, opts);
  81. var propertyArgv = parse(regular, opts);
  82. var expected = {
  83. herp: true,
  84. h: true,
  85. '_': [ ]
  86. };
  87. t.same(aliasedArgv, expected);
  88. t.same(propertyArgv, expected);
  89. t.end();
  90. });
  91. // regression, see https://github.com/substack/node-optimist/issues/71
  92. test('boolean and --x=true', function(t) {
  93. var parsed = parse(['--boool', '--other=true'], {
  94. boolean: 'boool'
  95. });
  96. t.same(parsed.boool, true);
  97. t.same(parsed.other, 'true');
  98. parsed = parse(['--boool', '--other=false'], {
  99. boolean: 'boool'
  100. });
  101. t.same(parsed.boool, true);
  102. t.same(parsed.other, 'false');
  103. t.end();
  104. });