Enapso Smart Arguments Processor
npm install @marcelbraun/enapso-argsYou can either pass a single object as arguments or the arguments in the defined order.
You avoid unexpected results due to wrong or missing arguments or due to wrong argument order.
You can specify defaults for missing arguments and valdiate your arguments against a given schema.
npm i @marcelbraun/enapso-args --save
`Usage
Initialization
`javascript
const
{ EnapsoArguments } = require('@marcelbraun/enapso-args')
;global.enargs = new EnapsoArguments();
global.enGetArgs = enargs.getArgs;
`Arg Validation Schema
`javascript
const
schemaSingleStringArg = {
args: [
{
"identifier": "text",
"type": "string",
"required": false,
"default": "default value",
"minLength": 0,
"maxLength": 20
}
]
};
`
Argument Validation
`javascriptfunction demoSingleStringArg(/text/) {
let args = enGetArgs(arguments, schemaSingleStringArg, logValidationViolation);
console.log('demoSingleStringArg: ' + args.text);
}
function demo() {
demoSingleStringArg("Test");
demoSingleStringArg(1);
demoSingleStringArg(true);
demoSingleStringArg();
}
`
Output
`
demoSingleStringArg: Test
Error: Argument 'text' must be of type 'string' but is of type 'number'. Value: 1
demoSingleStringArg: 1
Error: Argument 'text' must be of type 'string' but is of type 'boolean'. Value: true
demoSingleStringArg: true
demoSingleStringArg: default value
``