From ccdeb933492abf59e9178b9de5cfe21405c2f3cb Mon Sep 17 00:00:00 2001 From: C-3PO Date: Tue, 23 Oct 2018 17:46:52 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Allow=20custom=20error=20message=20?= =?UTF-8?q?in=20argument=20parser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/funcs/parseArguments.ts | 13 +++++++++++++ src/getManifest.ts | 2 +- src/getPatchZip.ts | 6 +++--- src/getSolidpkg.ts | 6 +++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/funcs/parseArguments.ts b/src/funcs/parseArguments.ts index 25c150d..f586469 100644 --- a/src/funcs/parseArguments.ts +++ b/src/funcs/parseArguments.ts @@ -12,6 +12,8 @@ interface IOption { description?: string; /** A function that verifies if the given value is a valid value for this option. If set to `undefined`, any value is accepted for this option. */ verify?: (value: string) => boolean; + /** A function returning a custom error message that is shown if the verify function returns false. If the function is `undefined`, or it returns `undefined` or an empty string, a default error message is shown instead. */ + message?: (value: string) => string; } /** @@ -70,8 +72,19 @@ export default function parseArguments(spec: { [key: string]: IOption }): { fail function parseValue(argumentValue: string) { //Verify value for correctness const verifyFunc = spec[argumentOption].verify; + const messageFunc = spec[argumentOption].message; try { if (verifyFunc !== undefined && !verifyFunc(argumentValue)) { + if (messageFunc !== undefined) { + try { + const customMessage = messageFunc(argumentValue); + if (customMessage !== undefined && customMessage !== '') { + failFunction(customMessage); + } + } catch (error) { + failFunction(`Invalid value set for option "${argumentOption}": "${argumentValue}". Could not show custom error message: ${String(error)}`); + } + } failFunction(`Invalid value set for option "${argumentOption}": "${argumentValue}".`); } } catch (error) { diff --git a/src/getManifest.ts b/src/getManifest.ts index 0e31524..ec57084 100644 --- a/src/getManifest.ts +++ b/src/getManifest.ts @@ -4,7 +4,7 @@ import { getManifest, IManifest, Product, verifyProductName } from 'ssn'; import parseArguments from './funcs/parseArguments'; const { args, fail } = parseArguments({ - product: { short: 'p', description: 'Name of the product we want to get the manifest on', verify: verifyProductName }, + product: { short: 'p', description: 'Name of the product we want to get the manifest on', verify: verifyProductName, message: (value) => `"${value}" is not a valid product name.` }, }); //Get manifest and write output to stdout diff --git a/src/getPatchZip.ts b/src/getPatchZip.ts index 78d191b..b996712 100644 --- a/src/getPatchZip.ts +++ b/src/getPatchZip.ts @@ -4,9 +4,9 @@ import { getPatchZip, ISsnFileEntry, Product, verifyProductName } from 'ssn'; import parseArguments from './funcs/parseArguments'; const { args, fail } = parseArguments({ - product: { short: 'p', description: 'Name of the product we want to get the patch info on', verify: verifyProductName }, - from: { short: 'f', description: 'Number of the from release', verify: (str) => str.match(/^(-1|0|[1-9][0-9]{0,2})$/) !== null }, - to: { short: 't', description: 'Number of the to release', verify: (str) => str.match(/^(0|[1-9][0-9]{0,2})$/) !== null }, + product: { short: 'p', description: 'Name of the product we want to get the patch info on', verify: verifyProductName, message: (value) => `"${value}" is not a valid product name.` }, + from: { short: 'f', description: 'Number of the from release, must be an integer in the range [-1, 999]', verify: (str) => str.match(/^(-1|0|[1-9][0-9]{0,2})$/) !== null }, + to: { short: 't', description: 'Number of the to release, must be an integer in the range [0, 999]', verify: (str) => str.match(/^(0|[1-9][0-9]{0,2})$/) !== null }, }); //Get the .zip file from this patch and write output to stdout diff --git a/src/getSolidpkg.ts b/src/getSolidpkg.ts index 3969abf..7dfb43c 100644 --- a/src/getSolidpkg.ts +++ b/src/getSolidpkg.ts @@ -4,9 +4,9 @@ import { getSolidpkg, ISolidSimple, Product, verifyProductName } from 'ssn'; import parseArguments from './funcs/parseArguments'; const { args, fail } = parseArguments({ - product: { short: 'p', description: 'Name of the product we want to get the solidpkg on', verify: verifyProductName }, - from: { short: 'f', description: 'Number of the from release', verify: (str) => str.match(/^(-1|0|[1-9][0-9]{0,2})$/) !== null }, - to: { short: 't', description: 'Number of the to release', verify: (str) => str.match(/^(0|[1-9][0-9]{0,2})$/) !== null }, + product: { short: 'p', description: 'Name of the product we want to get the solidpkg on', verify: verifyProductName, message: (value) => `"${value}" is not a valid product name.` }, + from: { short: 'f', description: 'Number of the from release, must be an integer in the range [-1, 999]', verify: (str) => str.match(/^(-1|0|[1-9][0-9]{0,2})$/) !== null }, + to: { short: 't', description: 'Number of the to release, must be an integer in the range [0, 999]', verify: (str) => str.match(/^(0|[1-9][0-9]{0,2})$/) !== null }, }); //Get solidpkg and write output to stdout