diff --git a/src/failWithError.ts b/src/failWithError.ts index 5dde098..fee5f06 100644 --- a/src/failWithError.ts +++ b/src/failWithError.ts @@ -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`); } - process.stderr.write(`Usage: ${usage} \n`); - process.exit(1); + if (usage !== '') { + process.stderr.write(`Usage: ${usage} \n`); + } + return process.exit(1); } diff --git a/src/index.ts b/src/index.ts index 627e388..0ba7f3f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 }); diff --git a/src/initState.ts b/src/initState.ts index 83fc796..ad02d68 100644 --- a/src/initState.ts +++ b/src/initState.ts @@ -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 }; } diff --git a/src/parseArgument.ts b/src/parseArgument.ts index ec8704c..c4fb3d8 100644 --- a/src/parseArgument.ts +++ b/src/parseArgument.ts @@ -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 }, diff --git a/src/runPreParseHook.ts b/src/runPreParseHook.ts index e364034..15d8a4d 100644 --- a/src/runPreParseHook.ts +++ b/src/runPreParseHook.ts @@ -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; diff --git a/src/verifyAndStoreOptionValue.ts b/src/verifyAndStoreOptionValue.ts index f6e17ff..673d479 100644 --- a/src/verifyAndStoreOptionValue.ts +++ b/src/verifyAndStoreOptionValue.ts @@ -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 }, }) {