-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathParseCommandLine.js
More file actions
188 lines (164 loc) · 6.12 KB
/
ParseCommandLine.js
File metadata and controls
188 lines (164 loc) · 6.12 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* global */
// Function to parse a commandline
// The line is broken into individual tokens
// Each token that is recognized as a JS type will get that type
// Otherwise the token is considered to be a string
// name-value pairs in the form "name=value" are added to the "argsObject" dictionary
// other parameters are added to the "argsArray" array
// e.g.:
// test "1 2 3" 4 x=7 {"a":1, "b":2}
// is a command line of 5 tokens
// string: "test"
// string: "1 2 3"
// number: 4
// number: 7
// dictionary: {"a":1, "b": 2}
// the array will be filled with 4 elements
// the dictionary will be filled with one element named "x"
import {Character} from "./Character.js";
export class ParseCommandLine {
static getPatJid () {
return /^[2-9]\d\d\d[01]\d[0-3]\d[0-2]\d[0-5]\d[0-5]\d\d\d\d\d\d\d$/;
}
static getCommandFromCommandLine (pCommandLine) {
const argsArray = [];
const argsObject = {};
ParseCommandLine.parseCommandLine(pCommandLine, argsArray, argsObject);
return argsArray[0];
}
static parseCommandLine (pToRun, pArgsArray, pArgsObject) {
const patPlaceHolder = /^<[a-z]+>/;
// note that "none" is not case-insensitive, but "null" is
const patNull = /^(?:None|null|Null|NULL)$/;
const patBooleanFalse = /^(?:false|False|FALSE)$/;
const patBooleanTrue = /^(?:true|True|TRUE)$/;
const patInteger = /^(?:(?:0)|(?:[-+]?[1-9]\d*))$/;
const patFloat = /^(?:[-+]?(?:(?:\d+)|(?:\d+[.]\d*)|(?:\d*[.]\d+))(?:[eE][-+]?\d+)?)$/;
// just in case the user typed some extra whitespace
// at the start of the line
pToRun = pToRun.trim();
while (pToRun.length > 0) {
let name = null;
let firstSpaceChar = pToRun.indexOf(" ");
if (firstSpaceChar < 0) {
firstSpaceChar = pToRun.length;
}
const firstEqualSign = pToRun.indexOf("=");
if (firstEqualSign >= 0 && firstEqualSign < firstSpaceChar) {
// we have the name of a named parameter
name = pToRun.substring(0, firstEqualSign);
pToRun = pToRun.substring(firstEqualSign + 1);
if (pToRun === "" || pToRun[0] === " ") {
return "Must have value for named parameter '" + name + "'";
}
}
if (patPlaceHolder.test(pToRun)) {
const placeHolder = pToRun.replace(/>.*/, ">");
return "Must fill in all placeholders, e.g. " + placeHolder;
}
// Determine whether the JSON string starts with a known
// character for a JSON type
let beginChar;
let endChar;
let objType;
if (pToRun[0] === "{") {
beginChar = "{";
endChar = "}";
objType = "dictionary";
} else if (pToRun[0] === "[") {
beginChar = "[";
endChar = "]";
objType = "array";
} else if (pToRun.startsWith("\"\"\"")) {
beginChar = "\"\"\"";
endChar = "\"\"\"";
objType = "triple-quoted-string";
} else if (pToRun[0] === "\"") {
// note that json does not support single-quoted strings
beginChar = "\"";
endChar = "\"";
objType = "double-quoted-string";
}
let value;
if (endChar && objType) {
// The string starts with a character for a known JSON type
let charPos = beginChar.length;
for (;;) {
// Try until the next closing character
let endCharPos = pToRun.indexOf(endChar, charPos);
if (endCharPos < 0) {
return "No valid " + objType + " found";
}
// parse what we have found so far
// the string ends with a closing character
// but that may not be enough, e.g. "{a:{}"
try {
if (objType === "triple-quoted-string") {
value = pToRun.substring(beginChar.length, endCharPos);
} else {
const fndStr = pToRun.substring(0, endCharPos + endChar.length);
value = JSON.parse(fndStr);
}
} catch (err) { // eslint-disable-line no-unused-vars
// the string that we tried to parse is not valid json
// continue to add more text from the input
charPos = endCharPos + 1;
continue;
}
// the first part of the string is valid JSON
endCharPos += endChar.length;
if (endCharPos < pToRun.length && pToRun[endCharPos] !== " ") {
return "Valid " + objType + ", but followed by text:" + pToRun.substring(endCharPos) + Character.HORIZONTAL_ELLIPSIS;
}
// valid JSON and not followed by strange characters
pToRun = pToRun.substring(endCharPos);
break;
}
} else {
// everything else is a string (without quotes)
// when we are done, we'll see whether it actually is a number
// or any of the known constants
let str = "";
while (pToRun.length > 0 && pToRun[0] !== " ") {
str += pToRun[0];
pToRun = pToRun.substring(1);
}
// try to find whether the string is actually a known constant
// or integer or float
if (patNull.test(str)) {
value = null;
} else if (patBooleanFalse.test(str)) {
value = false;
} else if (patBooleanTrue.test(str)) {
value = true;
} else if (ParseCommandLine.getPatJid().test(str)) {
// jobIds look like numbers but must be strings
value = str;
} else if (patInteger.test(str)) {
value = parseInt(str, 10);
} else if (patFloat.test(str)) {
value = parseFloat(str);
if (!isFinite(value)) {
return "Numeric argument has overflowed or is infinity";
}
} else {
value = str;
}
}
if (name === null) {
// anonymous parameter
pArgsArray.push(value);
} else if (name in pArgsObject) {
// named parameter which already exists
return "Duplicate named variable '" + name + "'";
} else {
// named parameter
pArgsObject[name] = value;
}
// ignore the whitespace before the next part
pToRun = pToRun.trim();
}
// succesfull (no error message return)
return null;
}
}