argument-parser/src/parseSpecEntry.ts

25 lines
1.3 KiB
TypeScript

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]+)*$/) === null) {
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;
}
}