-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchCommand.js
60 lines (51 loc) · 1.38 KB
/
matchCommand.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
module.exports = (branch, cmd) => {
var res = {
positionals: {},
flags: {},
options: {},
acc: ''
};
if (branch.accumulate) {
if (branch.specifiers.length + branch.positionals.length + branch.flags.length + branch.options.length == 0) {
res.acc = cmd.val;
return res;
}
if (branch.specifiers.length + branch.positionals.length > cmd.positionals.length) {
return null;
}
} else if (branch.specifiers.length + branch.positionals.length != cmd.positionals.length) {
return null;
}
for (let i of cmd.flags) {
if (!branch.flags.includes(i)) {
return null;
}
}
for (let i of branch.flags) {
res.flags[i] = cmd.flags.includes(i);
}
for (let i in cmd.options) {
if (!branch.options.includes(i)) {
return null;
}
res.options[i] = cmd.options[i];
}
for (let i in branch.specifiers) {
if (branch.specifiers[i] != cmd.positionals[i]) {
return null;
}
}
for (let i in branch.positionals) {
let p = cmd.positionals.slice(branch.specifiers.length);
res.positionals[branch.positionals[i]] = p[i];
}
if (branch.accumulate) {
acc = ''
for (i = branch.specifiers.length + branch.positionals.length; i < cmd.positionals.length; ++i) {
acc += cmd.positionals[i] + ' ';
}
acc = acc.slice(0, acc.length-1);
res.acc = acc;
}
return res;
}