UNPKG

6.25 kBJavaScriptView Raw
1'use strict'
2const TestRunner = require('test-runner')
3const commandLineArgs = require('../')
4const a = require('assert')
5
6const runner = new TestRunner()
7
8runner.test('partial: simple', function () {
9 const definitions = [
10 { name: 'one', type: Boolean }
11 ]
12 const argv = [ '--two', 'two', '--one', 'two' ]
13 const options = commandLineArgs(definitions, { argv, partial: true })
14 a.deepStrictEqual(options, {
15 one: true,
16 _unknown: [ '--two', 'two', 'two' ]
17 })
18})
19
20runner.test('partial: defaultOption', function () {
21 const definitions = [
22 { name: 'files', type: String, defaultOption: true, multiple: true }
23 ]
24 const argv = [ '--files', 'file1', '--one', 'file2' ]
25 const options = commandLineArgs(definitions, { argv, partial: true })
26 a.deepStrictEqual(options, {
27 files: [ 'file1', 'file2' ],
28 _unknown: [ '--one' ]
29 })
30})
31
32runner.test('partial: defaultOption 2', function () {
33 const definitions = [
34 { name: 'files', type: String, defaultOption: true, multiple: true },
35 { name: 'one', type: Boolean },
36 { name: 'two', alias: 't', defaultValue: 2 }
37 ]
38 const argv = [ 'file1', '--one', 'file2', '-t', '--two=3', 'file3', '-ab' ]
39 const options = commandLineArgs(definitions, { argv, partial: true })
40 a.deepStrictEqual(options, {
41 files: [ 'file1', 'file2', 'file3' ],
42 two: '3',
43 one: true,
44 _unknown: [ '-a', '-b' ]
45 })
46})
47
48runner.test('partial: defaultOption with value equal to defaultValue', function () {
49 const definitions = [
50 { name: 'file', type: String, defaultOption: true, defaultValue: 'file1' }
51 ]
52 const argv = [ 'file1', '--two=3', '--four', '5' ]
53 const options = commandLineArgs(definitions, { argv, partial: true })
54 a.deepStrictEqual(options, {
55 file: 'file1',
56 _unknown: [ '--two', '3', '--four', '5' ]
57 })
58})
59
60runner.test('partial: defaultOption with value equal to defaultValue 2', function () {
61 const definitions = [
62 { name: 'file', type: String, defaultOption: true, defaultValue: 'file1' }
63 ]
64 const argv = [ '--file', '--file=file1', '--two=3', '--four', '5' ]
65 const options = commandLineArgs(definitions, { argv, partial: true })
66 a.deepStrictEqual(options, {
67 file: 'file1',
68 _unknown: [ '--two', '3', '--four', '5' ]
69 })
70})
71
72runner.test('partial: multiple', function () {
73 const definitions = [
74 { name: 'files', type: String, multiple: true }
75 ]
76 const argv = [ 'file1', '--files', 'file2', '-t', '--two=3', 'file3', '-ab', '--files=file4' ]
77 const options = commandLineArgs(definitions, { argv, partial: true })
78 a.deepStrictEqual(options, {
79 files: [ 'file2', 'file4' ],
80 _unknown: [ 'file1', '-t', '--two=3', 'file3', '-a', '-b' ]
81 })
82})
83
84runner.test('unknown options: rejected defaultOption values end up in _unknown', function () {
85 const definitions = [
86 { name: 'foo', type: String },
87 { name: 'verbose', alias: 'v', type: Boolean },
88 { name: 'libs', type: String, defaultOption: true }
89 ]
90 const argv = [ '--foo', 'bar', '-v', 'libfn', '--libarg', 'val1', '-r' ]
91 const options = commandLineArgs(definitions, { argv, partial: true })
92 a.deepStrictEqual(options, {
93 foo: 'bar',
94 verbose: true,
95 libs: 'libfn',
96 _unknown: [ '--libarg', 'val1', '-r' ]
97 })
98})
99
100runner.test('partial: groups', function () {
101 const definitions = [
102 { name: 'one', group: 'a' },
103 { name: 'two', group: 'a' },
104 { name: 'three', group: 'b' }
105 ]
106 const argv = [ '--one', '1', '--two', '2', '--three', '3', 'ham', '--cheese' ]
107 a.deepStrictEqual(commandLineArgs(definitions, { argv, partial: true }), {
108 a: {
109 one: '1',
110 two: '2'
111 },
112 b: {
113 three: '3'
114 },
115 _all: {
116 one: '1',
117 two: '2',
118 three: '3'
119 },
120 _unknown: [ 'ham', '--cheese' ]
121 })
122})
123
124runner.test('partial: multiple groups and _none', function () {
125 const definitions = [
126 { name: 'one', group: ['a', 'f'] },
127 { name: 'two', group: ['a', 'g'] },
128 { name: 'three' }
129 ]
130 const argv = [ '--cheese', '--one', '1', 'ham', '--two', '2', '--three', '3', '-c' ]
131 a.deepStrictEqual(commandLineArgs(definitions, { argv, partial: true }), {
132 a: {
133 one: '1',
134 two: '2'
135 },
136 f: {
137 one: '1'
138 },
139 g: {
140 two: '2'
141 },
142 _none: {
143 three: '3'
144 },
145 _all: {
146 one: '1',
147 two: '2',
148 three: '3'
149 },
150 _unknown: [ '--cheese', 'ham', '-c' ]
151 })
152})
153
154runner.test('partial: defaultOption with --option=value notation', function () {
155 const definitions = [
156 { name: 'files', type: String, multiple: true, defaultOption: true }
157 ]
158 const argv = [ 'file1', 'file2', '--unknown=something' ]
159 const options = commandLineArgs(definitions, { argv, partial: true })
160 a.deepStrictEqual(options, {
161 files: [ 'file1', 'file2' ],
162 _unknown: [ '--unknown=something' ]
163 })
164})
165
166runner.test('partial: defaultOption with --option=value notation 2', function () {
167 const definitions = [
168 { name: 'files', type: String, multiple: true, defaultOption: true }
169 ]
170 const argv = [ 'file1', 'file2', '--unknown=something', '--files', 'file3', '--files=file4' ]
171 const options = commandLineArgs(definitions, { argv, partial: true })
172 a.deepStrictEqual(options, {
173 files: [ 'file1', 'file2', 'file3', 'file4' ],
174 _unknown: [ '--unknown=something' ]
175 })
176})
177
178runner.test('partial: defaultOption with --option=value notation 3', function () {
179 const definitions = [
180 { name: 'files', type: String, multiple: true, defaultOption: true }
181 ]
182 const argv = [ '--unknown', 'file1', '--another', 'something', 'file2', '--unknown=something', '--files', 'file3', '--files=file4' ]
183 const options = commandLineArgs(definitions, { argv, partial: true })
184 a.deepStrictEqual(options, {
185 files: [ 'file1', 'something', 'file2', 'file3', 'file4' ],
186 _unknown: [ '--unknown', '--another', '--unknown=something' ]
187 })
188})
189
190runner.test('partial: mulitple unknowns with same name', function () {
191 const definitions = [
192 { name: 'file' }
193 ]
194 const argv = [ '--unknown', '--unknown=something', '--file=file1', '--unknown' ]
195 const options = commandLineArgs(definitions, { argv, partial: true })
196 a.deepStrictEqual(options, {
197 file: 'file1',
198 _unknown: [ '--unknown', '--unknown=something', '--unknown' ]
199 })
200})