diff --git a/opter.js b/opter.js index 32e29a1..0f5f047 100644 --- a/opter.js +++ b/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; } } diff --git a/test.js b/test.js index 85c1265..a8ffe42 100644 --- a/test.js +++ b/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}'`,