-
Notifications
You must be signed in to change notification settings - Fork 2
/
string-input.js
161 lines (141 loc) · 3.82 KB
/
string-input.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"use strict";
const getOpts = require("../index.js");
suite("String input", () => {
const expect = (input, optdef, output) => {
const msg = `Parsing ${input}`;
assert.deepEqual(getOpts(input, optdef), output, msg);
};
test("Basic value: Single", () =>
expect("--foo", {"--foo": ""}, {
argv: [],
options: {foo: true},
}));
test("Basic value: Multiple", () => {
expect("--foo bar", {"--foo": ""}, {
argv: ["bar"],
options: {foo: true},
});
expect("--foo bar", {"--foo": "string"}, {
argv: [],
options: {foo: "bar"},
});
expect("--foo --bar baz", {
"--foo": "string",
"--bar": "string",
}, {
argv: [],
options: {
foo: undefined,
bar: "baz",
},
});
expect("--foo baz --bar qux", {
"--foo": "string",
"--bar": "string",
}, {
argv: [],
options: {
foo: "baz",
bar: "qux",
},
});
});
test("Quotes: Basic usage", () => {
expect('"--foo" "--bar"', {"--foo": "", "--bar": ""}, {
argv: [],
options: {
foo: true,
bar: true,
},
});
expect('"--foo --bar"', {"--foo": "", "--bar": ""}, {
argv: ["--foo --bar"],
options: {},
});
expect('--foo "bar baz"', {"--foo": "string"}, {
argv: [],
options: {foo: "bar baz"},
});
});
test("Quotes: Nested", () => {
expect("--foo \"--bar='qux'\"", {"--foo": ""}, {
argv: ["--bar='qux'"],
options: {foo: true},
});
expect("--foo baz --bar='qux \" \" qul' 123", {
"--foo": "string",
"--bar": "string",
}, {
argv: ["123"],
options: {
foo: "baz",
bar: 'qux " " qul',
},
});
});
test("Escapes: Delimiters", () => {
expect("--foo bar\\ baz", {"--foo": "string"}, {
argv: [],
options: {foo: "bar baz"},
});
expect("--foo bar\\ baz", {"--foo": ""}, {
argv: ["bar baz"],
options: {foo: true},
});
});
test("Escapes: Consecutive", () => {
expect("--foo bar\\ \\ baz", {"--foo": "string"}, {
argv: [],
options: {foo: "bar baz"},
});
expect("--foo bar\\ \\ baz", {"--foo": ""}, {
argv: ["bar baz"],
options: {foo: true},
});
});
test("Escapes: Bare quotes", () => {
expect("--foo bar\\'s baz", {"--foo": "string"}, {
argv: ["baz"],
options: {foo: "bar's"},
});
expect('--foo bar\\"s baz', {"--foo": "string"}, {
argv: ["baz"],
options: {foo: 'bar"s'},
});
expect('--title="Foo \\"Bar\\" Baz"', {"--title": "string"}, {
argv: [],
options: {title: 'Foo "Bar" Baz'},
});
});
test("Escapes: Between quotes", () => {
expect("--title='Foo\\'s Bar' bar", {"--title": "string"}, {
argv: ["bar"],
options: {title: "Foo's Bar"},
});
expect("--title='Foo \\\"Bar\\\" Baz'", {"--title": "string"}, {
argv: [],
options: {title: 'Foo \\"Bar\\" Baz'},
});
expect("--title '\\\"bar\\\"'", {"--title": "string"}, {
argv: [],
options: {title: '\\"bar\\"'},
});
expect('--title "\\\'bar\\\'"', {"--title": "string"}, {
argv: [],
options: {title: "\\'bar\\'"},
});
});
test("Escapes: Dashes", () => {
expect("bar\\-baz", {"-b, --baz": ""}, {argv: ["bar-baz"], options: {}});
expect('"bar\\-baz"', {"-b, --baz": ""}, {argv: ["bar\\-baz"], options: {}});
expect("\\--foo", {"--foo": ""}, {argv: [], options: {foo: true}});
expect("\\ --foo", {"--foo": ""}, {argv: [" --foo"], options: {}});
expect('"\\--foo"', {"--foo": ""}, {argv: ["\\--foo"], options: {}});
expect('"\\\\--foo"', {"--foo": ""}, {argv: ["\\--foo"], options: {}});
// Assert that parsing escaped option-strings won't affect arrays
expect(["\\--foo"], {"--foo": ""}, {argv: ["\\--foo"], options: {}});
expect(["\\ --foo"], {"--foo": ""}, {argv: ["\\ --foo"], options: {}});
expect(['"\\--foo"'], {"--foo": ""}, {argv: ['"\\--foo"'], options: {}});
expect(['"\\\\--foo"'], {"--foo": ""}, {argv: ['"\\\\--foo"'], options: {}});
});
});