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