🎨 Set return type of fail function to never

This commit is contained in:
C-3PO 2018-10-25 04:01:00 +02:00
parent 20315cc64f
commit e50b34c068
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
6 changed files with 13 additions and 9 deletions

View file

@ -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) { if (msg !== undefined) {
process.stderr.write(`Error: ${String(msg).trim()}\n`); process.stderr.write(`Error: ${String(msg).trim()}\n`);
} }
if (usage !== '') {
process.stderr.write(`Usage: ${usage} \n`); process.stderr.write(`Usage: ${usage} \n`);
process.exit(1); }
return process.exit(1);
} }

View file

@ -12,7 +12,7 @@ import runPreParseHook from './runPreParseHook';
export default function parseArguments( export default function parseArguments(
spec: { [key: string]: IOption }, spec: { [key: string]: IOption },
preParseHook?: (args: string[], fail: (message: string) => void) => (string[] | undefined), 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 //Initialize state
const { outputArgs, shortToLongLookup, requiredOptions, fail } = initState(spec); const { outputArgs, shortToLongLookup, requiredOptions, fail } = initState(spec);
const args = runPreParseHook({ args: process.argv.slice(2), preParseHook, fail }); const args = runPreParseHook({ args: process.argv.slice(2), preParseHook, fail });

View file

@ -1,7 +1,7 @@
import failWithError from './failWithError'; import failWithError from './failWithError';
import IOption from './IOption'; 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 }) { export default function initState(spec: { [key: string]: IOption }) {
//The parsed arguments that are returned by this function //The parsed arguments that are returned by this function
@ -43,7 +43,7 @@ export default function initState(spec: { [key: string]: IOption }) {
)).join('\n') )).join('\n')
}`; }`;
const failWithUsage = failWithError.bind(null, usage); const failWithUsage: (error?: string) => never = failWithError.bind(null, usage);
return { outputArgs, shortToLongLookup, requiredOptions, fail: failWithUsage }; return { outputArgs, shortToLongLookup, requiredOptions, fail: failWithUsage };
} }

View file

@ -4,7 +4,7 @@ import verifyAndStoreOptionValue from './verifyAndStoreOptionValue';
export default function parseArgument( export default function parseArgument(
{ spec, fail, outputArgs, requiredOptions, shortToLongLookup }: { { spec, fail, outputArgs, requiredOptions, shortToLongLookup }: {
spec: { [key: string]: IOption }, spec: { [key: string]: IOption },
fail: (errorMessage: string) => void, fail: (errorMessage: string) => never,
outputArgs: { [key: string]: string }, outputArgs: { [key: string]: string },
requiredOptions: { [key: string]: boolean }, requiredOptions: { [key: string]: boolean },
shortToLongLookup: { [key: string]: string }, shortToLongLookup: { [key: string]: string },

View file

@ -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 }: { export default function runPreParseHook({ args, preParseHook, fail }: {
args: string[], args: string[],
preParseHook?: (args: string[], fail: (message: string) => void) => (string[] | undefined), preParseHook?: (args: string[], fail: (message: string) => void) => (string[] | undefined),
fail: (errorMessage: string) => void, fail: (errorMessage: string) => never,
}): string[] { }): string[] {
if (preParseHook === undefined) { if (preParseHook === undefined) {
return args; return args;

View file

@ -13,7 +13,7 @@ export default function verifyAndStoreOptionValue({
value: string, value: string,
verify: IOption['verify'], verify: IOption['verify'],
message: IOption['message'], message: IOption['message'],
fail: (errorMessage: string) => void, fail: (errorMessage: string) => never,
outputArgs: { [key: string]: string }, outputArgs: { [key: string]: string },
requiredOptions: { [key: string]: boolean }, requiredOptions: { [key: string]: boolean },
}) { }) {