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