Fix: Check for synonyms when checking for required arguments
This commit is contained in:
parent
b9e15019c0
commit
e5e1eeec8a
23
opter.js
23
opter.js
|
|
@ -49,15 +49,11 @@ module.exports = class Opts {
|
|||
**/
|
||||
requireOption(nameWithDashes, errorMessage=null) {
|
||||
const name = stripDashes(nameWithDashes);
|
||||
console.log('req opt name:', name);
|
||||
if (name in this.opts) return true;
|
||||
if (name in this.synonyms) {
|
||||
let syns = this.synonyms[name];
|
||||
console.log('syns', syns);
|
||||
let hits = syns.filter(s => s in this.opts ).length;
|
||||
if (hits > 0) {
|
||||
return true;
|
||||
}
|
||||
if (hits > 0) { return true; }
|
||||
}
|
||||
throw new Error(errorMessage || defaultErrorMessages.missingRequiredOption(nameWithDashes));
|
||||
}
|
||||
|
|
@ -74,10 +70,8 @@ module.exports = class Opts {
|
|||
**/
|
||||
requireOptionArgument(nameWithDashes, minRequired=1, maxRequired=1, tooFewMessage=null, tooManyMessage=null) {
|
||||
let name = stripDashes(nameWithDashes);
|
||||
// FIXME have to check synonyms, too
|
||||
if (name in this.opts) {
|
||||
console.log('type', typeof this.opts[name] === 'boolean');
|
||||
console.log(this.opts[name]);
|
||||
|
||||
const checkArgs = (name) => {
|
||||
if (typeof this.opts[name] === 'boolean') {
|
||||
throw new Error(tooFewMessage || defaultErrorMessages.missingRequiredArgument(nameWithDashes));
|
||||
} else if (this.opts[name].length < minRequired) {
|
||||
|
|
@ -86,14 +80,21 @@ module.exports = class Opts {
|
|||
throw new Error(tooManyMessage || defaultErrorMessages.tooManyArguments(nameWithDashes));
|
||||
}
|
||||
}
|
||||
|
||||
if (name in this.opts) {
|
||||
checkArgs(this.opts[name]);
|
||||
}
|
||||
if (name in this.synonyms) {
|
||||
const syn = this.synonyms[name].filter(s => s in this.opts )[0];
|
||||
checkArgs(syn);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
contains(nameWithDashes) {
|
||||
const name = stripDashes(nameWithDashes);
|
||||
console.log('synonyms:', this.synonyms);
|
||||
if (name in this.opts) return true;
|
||||
if (name in this.synonyms) return true;
|
||||
if (name in this.synonyms) return true; // FIXME
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
5
test.js
5
test.js
|
|
@ -10,8 +10,9 @@ const opts = new Opter(process.argv)
|
|||
opts.synonymize('-i', '--input');
|
||||
opts.synonymize('-d', '--debug');
|
||||
opts.requireOption('-i');
|
||||
opts.requireOptionArgument('-i');
|
||||
console.log('contains', opts.contains('-d'));
|
||||
opts.requireOptionArgument('--input');
|
||||
console.log('contains --debug', opts.contains('-d'));
|
||||
console.log('contains --input', opts.contains('--input'));
|
||||
|
||||
// const _defaultErrorMessages = {
|
||||
// missingRequiredOption: (optionName) => `Missing required option '${optionName}'`,
|
||||
|
|
|
|||
Loading…
Reference in New Issue