🎨 Set return type of fail function to never
This commit is contained in:
parent
20315cc64f
commit
e50b34c068
6 changed files with 13 additions and 9 deletions
|
@ -1,7 +1,10 @@
|
|||
export default function failWithError(usage: string, msg?: string) {
|
||||
/** Prints the given error message and usage instructions, then terminates with a non-zero exit code. */
|
||||
export default function failWithError(usage: string, msg?: string): never {
|
||||
if (msg !== undefined) {
|
||||
process.stderr.write(`Error: ${String(msg).trim()}\n`);
|
||||
}
|
||||
if (usage !== '') {
|
||||
process.stderr.write(`Usage: ${usage} \n`);
|
||||
process.exit(1);
|
||||
}
|
||||
return process.exit(1);
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ import runPreParseHook from './runPreParseHook';
|
|||
export default function parseArguments(
|
||||
spec: { [key: string]: IOption },
|
||||
preParseHook?: (args: string[], fail: (message: string) => void) => (string[] | undefined),
|
||||
): { fail: (errorMessage: string) => void, args: { [key: string]: string } } {
|
||||
): { fail: (errorMessage: string) => never, args: { [key: string]: string } } {
|
||||
//Initialize state
|
||||
const { outputArgs, shortToLongLookup, requiredOptions, fail } = initState(spec);
|
||||
const args = runPreParseHook({ args: process.argv.slice(2), preParseHook, fail });
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import failWithError from './failWithError';
|
||||
import IOption from './IOption';
|
||||
|
||||
const failFunction = failWithError.bind(null, '');
|
||||
const failFunction: (error?: string) => never = failWithError.bind(null, '');
|
||||
|
||||
export default function initState(spec: { [key: string]: IOption }) {
|
||||
//The parsed arguments that are returned by this function
|
||||
|
@ -43,7 +43,7 @@ export default function initState(spec: { [key: string]: IOption }) {
|
|||
)).join('\n')
|
||||
}`;
|
||||
|
||||
const failWithUsage = failWithError.bind(null, usage);
|
||||
const failWithUsage: (error?: string) => never = failWithError.bind(null, usage);
|
||||
|
||||
return { outputArgs, shortToLongLookup, requiredOptions, fail: failWithUsage };
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import verifyAndStoreOptionValue from './verifyAndStoreOptionValue';
|
|||
export default function parseArgument(
|
||||
{ spec, fail, outputArgs, requiredOptions, shortToLongLookup }: {
|
||||
spec: { [key: string]: IOption },
|
||||
fail: (errorMessage: string) => void,
|
||||
fail: (errorMessage: string) => never,
|
||||
outputArgs: { [key: string]: string },
|
||||
requiredOptions: { [key: string]: boolean },
|
||||
shortToLongLookup: { [key: string]: string },
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
/** Runs the given pre-parse hook, if defined, to modify the command line arguments. Returns the modified command line arguments. */
|
||||
export default function runPreParseHook({ args, preParseHook, fail }: {
|
||||
args: string[],
|
||||
preParseHook?: (args: string[], fail: (message: string) => void) => (string[] | undefined),
|
||||
fail: (errorMessage: string) => void,
|
||||
fail: (errorMessage: string) => never,
|
||||
}): string[] {
|
||||
if (preParseHook === undefined) {
|
||||
return args;
|
||||
|
|
|
@ -13,7 +13,7 @@ export default function verifyAndStoreOptionValue({
|
|||
value: string,
|
||||
verify: IOption['verify'],
|
||||
message: IOption['message'],
|
||||
fail: (errorMessage: string) => void,
|
||||
fail: (errorMessage: string) => never,
|
||||
outputArgs: { [key: string]: string },
|
||||
requiredOptions: { [key: string]: boolean },
|
||||
}) {
|
||||
|
|
Loading…
Reference in a new issue