Skip to content

Commit

Permalink
some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alicewriteswrongs committed Nov 3, 2022
1 parent 59cd113 commit 9aa5c73
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions src/cli/parse-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const parseFlags = (args: string[], _sys?: CompilerSystem): ConfigFlags =
* | /^[a-zA-Z0-9]+$/ ;
*
* There are additional constraints (not shown in the grammar for brevity's sake)
* on the type of CLIValue which will be associated with a particular argument.
* on the type of `CLIValue` which will be associated with a particular argument.
* We enforce this by declaring lists of boolean, string, etc arguments and
* checking that types of values before setting them.
*
Expand Down Expand Up @@ -177,7 +177,7 @@ const parseCLITerm = (flags: ConfigFlags, args: string[]) => {

/**
* Normalize a 'negative' flag name, just to do a little pre-processing before
* we pass it to `setValue`.
* we pass it to `setCLIArg`.
*
* @param flagName the flag name to normalize
* @returns a normalized flag name
Expand Down Expand Up @@ -215,7 +215,8 @@ const normalizeFlagName = (flagName: string): string => {
*
* @param flags a {@link ConfigFlags} object
* @param rawArg the raw argument name matched by the parser
* @param normalizedArg an argument with leading control characters (`--`, `--no-`, etc) removed
* @param normalizedArg an argument with leading control characters (`--`,
* `--no-`, etc) removed
* @param value the raw value to be set onto the config flags object
*/
const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, value: CLIValueResult) => {
Expand All @@ -228,21 +229,15 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va
: // no value was supplied, default to true
true;
flags.knownArgs.push(rawArg);
return;
}

if (readOnlyArrayHasStringMember(STRING_CLI_FLAGS, normalizedArg)) {
} else if (readOnlyArrayHasStringMember(STRING_CLI_FLAGS, normalizedArg)) {
if (typeof value === 'string') {
flags[normalizedArg] = value;
flags.knownArgs.push(rawArg);
flags.knownArgs.push(value);
} else {
throwCLIParsingError(rawArg, 'expected a string argument but received nothing');
}
return;
}

if (readOnlyArrayHasStringMember(STRING_ARRAY_CLI_FLAGS, normalizedArg)) {
} else if (readOnlyArrayHasStringMember(STRING_ARRAY_CLI_FLAGS, normalizedArg)) {
if (typeof value === 'string') {
if (!Array.isArray(flags[normalizedArg])) {
flags[normalizedArg] = [];
Expand All @@ -261,10 +256,7 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va
} else {
throwCLIParsingError(rawArg, 'expected a string argument but received nothing');
}
return;
}

if (readOnlyArrayHasStringMember(NUMBER_CLI_FLAGS, normalizedArg)) {
} else if (readOnlyArrayHasStringMember(NUMBER_CLI_FLAGS, normalizedArg)) {
if (typeof value === 'string') {
const parsed = parseInt(value, 10);

Expand All @@ -280,10 +272,7 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va
} else {
throwCLIParsingError(rawArg, 'expected a number argument but received nothing');
}
return;
}

if (readOnlyArrayHasStringMember(STRING_NUMBER_CLI_FLAGS, normalizedArg)) {
} else if (readOnlyArrayHasStringMember(STRING_NUMBER_CLI_FLAGS, normalizedArg)) {
if (typeof value === 'string') {
if (CLI_ARG_STRING_REGEX.test(value)) {
// if it matches the regex we treat it like a string
Expand All @@ -306,10 +295,7 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va
} else {
throwCLIParsingError(rawArg, 'expected a string or a number but received nothing');
}
return;
}

if (readOnlyArrayHasStringMember(LOG_LEVEL_CLI_FLAGS, normalizedArg)) {
} else if (readOnlyArrayHasStringMember(LOG_LEVEL_CLI_FLAGS, normalizedArg)) {
if (typeof value === 'string') {
if (isLogLevel(value)) {
flags[normalizedArg] = value;
Expand All @@ -321,7 +307,6 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va
} else {
throwCLIParsingError(rawArg, 'expected to receive a valid log level but received nothing');
}
return;
}
};

Expand All @@ -347,9 +332,9 @@ const CLI_ARG_STRING_REGEX = /[^\d\.Ee\+\-]+/g;
export const Empty = Symbol('Empty');

/**
* The result of trying to parse a CLI arg. `ArgValue` wraps up an argument if
* present, while `Empty` indicates that nothing was matched or that the input
* was malformed.
* The result of trying to parse a CLI arg. This will be a `string` if a
* well-formed value is present, or `Empty` to indicate that nothing was matched
* or that the input was malformed.
*/
type CLIValueResult = string | typeof Empty;

Expand Down Expand Up @@ -377,6 +362,7 @@ const parseCLIValue = (args: string[]): CLIValueResult => {
// until later on.
const value = args.shift();
if (typeof value === 'string') {
// this `if { }` block is just to keep TS happy :/
return value;
}
}
Expand Down Expand Up @@ -434,7 +420,8 @@ const isLogLevel = (maybeLogLevel: string): maybeLogLevel is LogLevel =>
readOnlyArrayHasStringMember(LOG_LEVELS, maybeLogLevel);

/**
* A little helper for constructing and throwing an error message with info about what went wrong
* A little helper for constructing and throwing an error message with info
* about what went wrong
*
* @param flag the flag which encountered the error
* @param message a message specific to the error which was encountered
Expand All @@ -447,7 +434,7 @@ const throwCLIParsingError = (flag: string, message: string) => {
* Throw a specific error for the situation where we ran into an issue parsing
* a number.
*
* @param flag the flag for which we encounted the issue
* @param flag the flag for which we encountered the issue
* @param value what we were trying to parse
*/
const throwNumberParsingError = (flag: string, value: string) => {
Expand All @@ -456,11 +443,12 @@ const throwNumberParsingError = (flag: string, value: string) => {

/**
* A little helper to 'dereference' a flag alias, which if you squint a little
* you can think of like a pointer to a full flag name. This 'c' is like a
* you can think of like a pointer to a full flag name. Thus 'c' is like a
* pointer to 'config', so here we're doing something like `*c`. Of course, this
* being JS, this is just a metaphor!
*
* If no 'dereference' is found for the possible alias we just return the passed string unmodified.
* If no 'dereference' is found for the possible alias we just return the
* passed string unmodified.
*
* @param maybeAlias a string which _could_ be an alias to a full flag name
* @returns the full aliased flag name, if found, or the passed string if not
Expand Down

0 comments on commit 9aa5c73

Please sign in to comment.