1 | 'use strict'
|
2 | const TestRunner = require('test-runner')
|
3 | const commandLineArgs = require('../')
|
4 | const a = require('assert')
|
5 |
|
6 | const runner = new TestRunner()
|
7 |
|
8 | runner.test('getOpt short notation: two flags, one option', function () {
|
9 | const optionDefinitions = [
|
10 | { name: 'flagA', alias: 'a' },
|
11 | { name: 'flagB', alias: 'b' },
|
12 | { name: 'three', alias: 'c' }
|
13 | ]
|
14 |
|
15 | const argv = [ '-abc', 'yeah' ]
|
16 | a.deepStrictEqual(commandLineArgs(optionDefinitions, { argv }), {
|
17 | flagA: null,
|
18 | flagB: null,
|
19 | three: 'yeah'
|
20 | })
|
21 | })
|
22 |
|
23 | runner.test('option=value notation: two plus a regular notation', function () {
|
24 | const optionDefinitions = [
|
25 | { name: 'one' },
|
26 | { name: 'two' },
|
27 | { name: 'three' }
|
28 | ]
|
29 |
|
30 | const argv = [ '--one=1', '--two', '2', '--three=3' ]
|
31 | const result = commandLineArgs(optionDefinitions, { argv })
|
32 | a.strictEqual(result.one, '1')
|
33 | a.strictEqual(result.two, '2')
|
34 | a.strictEqual(result.three, '3')
|
35 | })
|
36 |
|
37 | runner.test('option=value notation: value contains "="', function () {
|
38 | const optionDefinitions = [
|
39 | { name: 'url' },
|
40 | { name: 'two' },
|
41 | { name: 'three' }
|
42 | ]
|
43 |
|
44 | let result = commandLineArgs(optionDefinitions, { argv: [ '--url=my-url?q=123', '--two', '2', '--three=3' ] })
|
45 | a.strictEqual(result.url, 'my-url?q=123')
|
46 | a.strictEqual(result.two, '2')
|
47 | a.strictEqual(result.three, '3')
|
48 |
|
49 | result = commandLineArgs(optionDefinitions, { argv: [ '--url=my-url?q=123=1' ] })
|
50 | a.strictEqual(result.url, 'my-url?q=123=1')
|
51 |
|
52 | result = commandLineArgs({ name: 'my-url' }, { argv: [ '--my-url=my-url?q=123=1' ] })
|
53 | a.strictEqual(result['my-url'], 'my-url?q=123=1')
|
54 | })
|