Skip to content

Commit 9cf2abd

Browse files
committed
Implement min/max length for prefix command
1 parent d47efac commit 9cf2abd

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

backend/src/plugin/core/commandEngine/parsing/prefixParser.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,12 @@ function readCommandArg(
187187
return true;
188188
}
189189

190-
const greedy = !("greedy" in option && option.greedy === false);
191-
192190
if (!(option.array ?? false)) {
193191
if (!reader.canRead()) {
194192
return null;
195193
}
196194

197-
const result = readCommandArgValue(reader, option.type, greedy);
195+
const result = readCommandArgValue(reader, option);
198196
reader.skipWhitespace();
199197

200198
return result;
@@ -205,7 +203,7 @@ function readCommandArg(
205203
while (reader.canRead() && !reader.match(ARRAY_TERMINATOR)) {
206204
const prevCursor = reader.cursor;
207205

208-
const item = readCommandArgValue(reader, option.type, greedy);
206+
const item = readCommandArgValue(reader, option);
209207

210208
if (item === null) {
211209
reader.cursor = prevCursor;
@@ -227,20 +225,37 @@ function readCommandArg(
227225

228226
function readCommandArgValue(
229227
reader: StringReader,
230-
type: Exclude<OptionType, OptionType.Flag>,
231-
greedy: boolean,
228+
option: Option,
232229
): AnyArgsValueItem | null {
233-
const terminator = greedy ? GREEDY_VALUE_TERMINATOR : undefined;
230+
const greedy = !("greedy" in option && option.greedy === false);
234231

235-
switch (type) {
232+
switch (option.type) {
233+
case OptionType.Flag:
234+
return true;
236235
case OptionType.Integer:
237236
return readInteger(reader);
238237
case OptionType.Number:
239238
return readNumber(reader);
240239
case OptionType.String: {
240+
const terminator =
241+
(option.greedy ?? true) ? GREEDY_VALUE_TERMINATOR : undefined;
241242
const result = readString(reader, terminator);
242243

243-
if (result?.length === 0) {
244+
if (result === null || result.length === 0) {
245+
return null;
246+
}
247+
248+
if (
249+
option.minLength !== undefined &&
250+
result.length < option.minLength
251+
) {
252+
return null;
253+
}
254+
255+
if (
256+
option.maxLength !== undefined &&
257+
result.length > option.maxLength
258+
) {
244259
return null;
245260
}
246261

0 commit comments

Comments
 (0)