♻ Replace for statements by functions

This commit is contained in:
C-3PO 2018-10-25 06:42:39 +02:00
parent 640c8df6bc
commit 3ac2874912
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
7 changed files with 56 additions and 44 deletions

25
src/parseSpecEntry.ts Normal file
View file

@ -0,0 +1,25 @@
import IOption from './interfaces/IOption';
import IState from './interfaces/IState';
export default function parseSpecEntry({ outputArgs, requiredOptions, shortToLongLookup }: IState, [longOption, { short: shortOption, default: defaultValue }]: [string, IOption]) {
if (longOption === '') {
throw new Error(`The long option may not be an empty string. This is a bug in the source code of the script that you tried to call.`);
}
if (!longOption.match(/^[a-z0-9]+(-[a-z0-9]+)*$/)) {
throw new Error(`The long option "${longOption}" must only contain alpha-numeric characters, optionally separated by hyphens. This is a bug in the source code of the script that you tried to call.`);
}
if (shortOption !== undefined && shortOption !== '') {
if (shortOption.length > 1) {
throw new Error(`Short options must only be one character long but the short option "${shortOption}" for "${longOption}" was ${shortOption.length} characters long. This is a bug in the source code of the script that you tried to call.`);
}
shortToLongLookup[shortOption] = longOption;
}
//If a default value is given, use the default value (it can be overridden later), otherwise mark argument as being required
if (defaultValue !== undefined) {
outputArgs[longOption] = defaultValue;
} else {
requiredOptions[longOption] = false;
}
}