-
Notifications
You must be signed in to change notification settings - Fork 109
/
unknown-options.js
76 lines (69 loc) · 2.39 KB
/
unknown-options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict'
const TestRunner = require('test-runner')
const commandLineArgs = require('../')
const a = require('assert')
const runner = new TestRunner()
runner.test('unknown option: simple', function () {
const definitions = [
{ name: 'one', type: Boolean }
]
const argv = [ '--two', 'two', '--one', 'two' ]
const options = commandLineArgs(definitions, { argv, partial: true })
a.deepStrictEqual(options, {
one: true,
_unknown: [ '--two', 'two', 'two' ]
})
})
runner.test('unknown option: defaultOption', function () {
const definitions = [
{ name: 'files', type: String, defaultOption: true, multiple: true }
]
const argv = [ '--files', 'file1', '--one', 'file2' ]
const options = commandLineArgs(definitions, { argv, partial: true })
a.deepStrictEqual(options, {
files: [ 'file1', 'file2' ],
_unknown: [ '--one' ]
})
})
runner.test('unknown option: defaultOption 2', function () {
const definitions = [
{ name: 'files', type: String, defaultOption: true, multiple: true },
{ name: 'one', type: Boolean },
{ name: 'two', alias: 't', defaultValue: 2 },
]
const argv = [ 'file1', '--one', 'file2', '-t', '--two=3', 'file3', '-ab' ]
const options = commandLineArgs(definitions, { argv, partial: true })
a.deepStrictEqual(options, {
files: [ 'file1', 'file2', 'file3' ],
two: '3',
one: true,
_unknown: [ '-a', '-b' ]
})
})
runner.test('unknown option: groups')
runner.test('unknown option: multiple', function () {
const definitions = [
{ name: 'files', type: String, multiple: true }
]
const argv = [ 'file1', '--files', 'file2', '-t', '--two=3', 'file3', '-ab', '--files=file4' ]
const options = commandLineArgs(definitions, { argv, partial: true })
a.deepStrictEqual(options, {
files: [ 'file2', 'file4' ],
_unknown: [ 'file1', '-t', '--two', '3', 'file3', '-a', '-b' ]
})
})
runner.test('unknown options: rejected defaultOption values end up in _unknown', function () {
const definitions = [
{ name: 'foo', type: String },
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'libs', type: String, defaultOption: true }
]
const argv = [ '--foo', 'bar', '-v', 'libfn', '--libarg', 'val1', '-r' ]
const options = commandLineArgs(definitions, { argv, partial: true })
a.deepStrictEqual(options, {
foo: 'bar',
verbose: true,
libs: 'libfn',
_unknown: [ '--libarg', 'val1', '-r' ]
})
})