✨ Allow custom error message in argument parser
This commit is contained in:
parent
cbead3f886
commit
ccdeb93349
4 changed files with 20 additions and 7 deletions
|
@ -12,6 +12,8 @@ interface IOption {
|
||||||
description?: string;
|
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. */
|
/** 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;
|
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) {
|
function parseValue(argumentValue: string) {
|
||||||
//Verify value for correctness
|
//Verify value for correctness
|
||||||
const verifyFunc = spec[argumentOption].verify;
|
const verifyFunc = spec[argumentOption].verify;
|
||||||
|
const messageFunc = spec[argumentOption].message;
|
||||||
try {
|
try {
|
||||||
if (verifyFunc !== undefined && !verifyFunc(argumentValue)) {
|
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}".`);
|
failFunction(`Invalid value set for option "${argumentOption}": "${argumentValue}".`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { getManifest, IManifest, Product, verifyProductName } from 'ssn';
|
||||||
import parseArguments from './funcs/parseArguments';
|
import parseArguments from './funcs/parseArguments';
|
||||||
|
|
||||||
const { args, fail } = 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
|
//Get manifest and write output to stdout
|
||||||
|
|
|
@ -4,9 +4,9 @@ import { getPatchZip, ISsnFileEntry, Product, verifyProductName } from 'ssn';
|
||||||
import parseArguments from './funcs/parseArguments';
|
import parseArguments from './funcs/parseArguments';
|
||||||
|
|
||||||
const { args, fail } = parseArguments({
|
const { args, fail } = parseArguments({
|
||||||
product: { short: 'p', description: 'Name of the product we want to get the patch info on', verify: verifyProductName },
|
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', verify: (str) => str.match(/^(-1|0|[1-9][0-9]{0,2})$/) !== null },
|
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', verify: (str) => str.match(/^(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
|
//Get the .zip file from this patch and write output to stdout
|
||||||
|
|
|
@ -4,9 +4,9 @@ import { getSolidpkg, ISolidSimple, Product, verifyProductName } from 'ssn';
|
||||||
import parseArguments from './funcs/parseArguments';
|
import parseArguments from './funcs/parseArguments';
|
||||||
|
|
||||||
const { args, fail } = parseArguments({
|
const { args, fail } = parseArguments({
|
||||||
product: { short: 'p', description: 'Name of the product we want to get the solidpkg on', verify: verifyProductName },
|
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', verify: (str) => str.match(/^(-1|0|[1-9][0-9]{0,2})$/) !== null },
|
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', verify: (str) => str.match(/^(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
|
//Get solidpkg and write output to stdout
|
||||||
|
|
Loading…
Reference in a new issue