Skip to content

Commit

Permalink
fix null check errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alicewriteswrongs committed Nov 3, 2022
1 parent 15c372d commit 1b67a3b
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions src/cli/parse-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ const parseCLITerm = (flags: ConfigFlags, args: string[]) => {
// pull off the first arg from the argument array
const arg = args.shift();

// array is empty, we're done!
if (arg === undefined) return;

// EqualsArg → "--" ArgName "=" CLIValue ;
if (arg.startsWith('--') && arg.includes('=')) {
// we're dealing with an EqualsArg, we have a special helper for that
Expand Down Expand Up @@ -216,9 +219,7 @@ const normalizeFlagName = (flagName: string): string => {
* @param value the raw value to be set onto the config flags object
*/
const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, value: CLIValueResult) => {
if (CLI_FLAG_ALIASES[normalizedArg] !== undefined) {
normalizedArg = CLI_FLAG_ALIASES[normalizedArg];
}
normalizedArg = dereferenceAlias(normalizedArg);

if (readOnlyArrayHasStringMember(BOOLEAN_CLI_FLAGS, normalizedArg)) {
flags[normalizedArg] =
Expand Down Expand Up @@ -247,9 +248,16 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va
flags[normalizedArg] = [];
}

flags[normalizedArg].push(value);
flags.knownArgs.push(rawArg);
flags.knownArgs.push(value);
const targetArray = flags[normalizedArg];
// this is irritating, but TS doesn't know that the `!Array.isArray`
// check above guarantees we have an array to work with here, and it
// doesn't want to narrow the type of `flags[normalizedArg]`, so we need
// to grab a reference to that array and then `Array.isArray` that. Bah!
if (Array.isArray(targetArray)) {
targetArray.push(value);
flags.knownArgs.push(rawArg);
flags.knownArgs.push(value);
}
} else {
throwCLIParsingError(rawArg, 'expected a string argument but received nothing');
}
Expand Down Expand Up @@ -361,12 +369,16 @@ const parseCLIValue = (args: string[]): CLIValueResult => {
if (args[0] === undefined) {
return Empty;
}

// all we're concerned with here is that it does not start with `"-"`,
// which would indicate it should be parsed as a CLI flag and not a value.
if (!args[0].startsWith('-')) {
// It's not a flag, so we return the value and defer any specific parsing
// until later on.
return args.shift();
const value = args.shift();
if (typeof value === 'string') {
return value;
}
}
return Empty;
};
Expand Down Expand Up @@ -441,3 +453,23 @@ const throwCLIParsingError = (flag: string, message: string) => {
const throwNumberParsingError = (flag: string, value: string) => {
throwCLIParsingError(flag, `expected a number but received "${value}"`);
};

/**
* 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
* 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.
*
* @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
*/
const dereferenceAlias = (maybeAlias: string): string => {
const possibleDereference = CLI_FLAG_ALIASES[maybeAlias];

if (typeof possibleDereference === 'string') {
return possibleDereference;
}
return maybeAlias;
};

0 comments on commit 1b67a3b

Please sign in to comment.