Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): make regexps constants #5595

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions cli/parse_args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ function parseBooleanString(value: unknown) {

const FLAG_REGEXP =
/^(?:-(?:(?<doubleDash>-)(?<negated>no-)?)?)(?<key>.+?)(?:=(?<value>.+?))?$/s;
const LETTER_REGEXP = /[A-Za-z]/;
const NUMBER_REGEXP = /-?\d+(\.\d*)?(e-?\d+)?$/;
const HYPHEN_REGEXP = /^(-|--)[^-]/;
const VALUE_REGEXP = /=(?<value>.+)/;
const FLAG_NAME_REGEXP = /^--[^=]+$/;
const IS_FLAG_REGEXP = /^-/;
const SPECIAL_CHAR_REGEXP = /\W/;

/**
* Take a set of command line arguments, optionally with a set of options, and
Expand Down Expand Up @@ -591,11 +598,12 @@ export function parseArgs<
!booleanSet.has(key) &&
!stringSet.has(key) &&
!aliasMap.has(key) &&
!(allBools && /^--[^=]+$/.test(arg)) &&
!(allBools && FLAG_NAME_REGEXP.test(arg)) &&
unknownFn?.(arg, key, value) === false
) {
return;
}

if (typeof value === "string" && !stringSet.has(key)) {
value = isNumber(value) ? Number(value) : value;
}
Expand Down Expand Up @@ -647,7 +655,7 @@ export function parseArgs<
!booleanSet.has(key) &&
!allBools &&
next &&
!/^-/.test(next) &&
!IS_FLAG_REGEXP.test(next) &&
(aliasMap.get(key)
? !aliasIsBoolean(aliasMap, booleanSet, key)
: true)
Expand Down Expand Up @@ -680,22 +688,21 @@ export function parseArgs<
continue;
}

if (/[A-Za-z]/.test(letter) && /=/.test(next)) {
setArgument(letter, next.split(/=(.+)/)[1]!, arg, true);
broken = true;
break;
}

if (
/[A-Za-z]/.test(letter) &&
/-?\d+(\.\d*)?(e-?\d+)?$/.test(next)
) {
setArgument(letter, next, arg, true);
broken = true;
break;
if (LETTER_REGEXP.test(letter)) {
const groups = VALUE_REGEXP.exec(next)?.groups;
if (groups) {
setArgument(letter, groups.value!, arg, true);
broken = true;
break;
}
if (NUMBER_REGEXP.test(next)) {
setArgument(letter, next, arg, true);
broken = true;
break;
}
}

if (letters[j + 1] && letters[j + 1]!.match(/\W/)) {
if (letters[j + 1] && letters[j + 1]!.match(SPECIAL_CHAR_REGEXP)) {
setArgument(letter, arg.slice(j + 2), arg, true);
broken = true;
break;
Expand All @@ -713,7 +720,7 @@ export function parseArgs<
const nextArg = args[i + 1];
if (
nextArg &&
!/^(-|--)[^-]/.test(nextArg) &&
!HYPHEN_REGEXP.test(nextArg) &&
!booleanSet.has(key) &&
(aliasMap.get(key)
? !aliasIsBoolean(aliasMap, booleanSet, key)
Expand Down