Last change
on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | export function parseCommand(cmd) {
|
---|
| 2 | const extraSpacesStrippedCommand = cmd.replace(/\s{2,}/g, ' ');
|
---|
| 3 | const splitCommand = extraSpacesStrippedCommand.split(/\s+(?![^[]*]|[^<]*>)/);
|
---|
| 4 | const bregex = /\.*[\][<>]/g;
|
---|
| 5 | const firstCommand = splitCommand.shift();
|
---|
| 6 | if (!firstCommand)
|
---|
| 7 | throw new Error(`No command found in: ${cmd}`);
|
---|
| 8 | const parsedCommand = {
|
---|
| 9 | cmd: firstCommand.replace(bregex, ''),
|
---|
| 10 | demanded: [],
|
---|
| 11 | optional: [],
|
---|
| 12 | };
|
---|
| 13 | splitCommand.forEach((cmd, i) => {
|
---|
| 14 | let variadic = false;
|
---|
| 15 | cmd = cmd.replace(/\s/g, '');
|
---|
| 16 | if (/\.+[\]>]/.test(cmd) && i === splitCommand.length - 1)
|
---|
| 17 | variadic = true;
|
---|
| 18 | if (/^\[/.test(cmd)) {
|
---|
| 19 | parsedCommand.optional.push({
|
---|
| 20 | cmd: cmd.replace(bregex, '').split('|'),
|
---|
| 21 | variadic,
|
---|
| 22 | });
|
---|
| 23 | }
|
---|
| 24 | else {
|
---|
| 25 | parsedCommand.demanded.push({
|
---|
| 26 | cmd: cmd.replace(bregex, '').split('|'),
|
---|
| 27 | variadic,
|
---|
| 28 | });
|
---|
| 29 | }
|
---|
| 30 | });
|
---|
| 31 | return parsedCommand;
|
---|
| 32 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.