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) {
|
requireOption(nameWithDashes, errorMessage=null) {
|
||||||
const name = stripDashes(nameWithDashes);
|
const name = stripDashes(nameWithDashes);
|
||||||
console.log('req opt name:', name);
|
|
||||||
if (name in this.opts) return true;
|
if (name in this.opts) return true;
|
||||||
if (name in this.synonyms) {
|
if (name in this.synonyms) {
|
||||||
let syns = this.synonyms[name];
|
let syns = this.synonyms[name];
|
||||||
console.log('syns', syns);
|
|
||||||
let hits = syns.filter(s => s in this.opts ).length;
|
let hits = syns.filter(s => s in this.opts ).length;
|
||||||
if (hits > 0) {
|
if (hits > 0) { return true; }
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
throw new Error(errorMessage || defaultErrorMessages.missingRequiredOption(nameWithDashes));
|
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) {
|
requireOptionArgument(nameWithDashes, minRequired=1, maxRequired=1, tooFewMessage=null, tooManyMessage=null) {
|
||||||
let name = stripDashes(nameWithDashes);
|
let name = stripDashes(nameWithDashes);
|
||||||
// FIXME have to check synonyms, too
|
|
||||||
if (name in this.opts) {
|
const checkArgs = (name) => {
|
||||||
console.log('type', typeof this.opts[name] === 'boolean');
|
|
||||||
console.log(this.opts[name]);
|
|
||||||
if (typeof this.opts[name] === 'boolean') {
|
if (typeof this.opts[name] === 'boolean') {
|
||||||
throw new Error(tooFewMessage || defaultErrorMessages.missingRequiredArgument(nameWithDashes));
|
throw new Error(tooFewMessage || defaultErrorMessages.missingRequiredArgument(nameWithDashes));
|
||||||
} else if (this.opts[name].length < minRequired) {
|
} else if (this.opts[name].length < minRequired) {
|
||||||
|
|
@ -86,14 +80,21 @@ module.exports = class Opts {
|
||||||
throw new Error(tooManyMessage || defaultErrorMessages.tooManyArguments(nameWithDashes));
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
contains(nameWithDashes) {
|
contains(nameWithDashes) {
|
||||||
const name = stripDashes(nameWithDashes);
|
const name = stripDashes(nameWithDashes);
|
||||||
console.log('synonyms:', this.synonyms);
|
|
||||||
if (name in this.opts) return true;
|
if (name in this.opts) return true;
|
||||||
if (name in this.synonyms) return true;
|
if (name in this.synonyms) return true; // FIXME
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
5
test.js
5
test.js
|
|
@ -10,8 +10,9 @@ const opts = new Opter(process.argv)
|
||||||
opts.synonymize('-i', '--input');
|
opts.synonymize('-i', '--input');
|
||||||
opts.synonymize('-d', '--debug');
|
opts.synonymize('-d', '--debug');
|
||||||
opts.requireOption('-i');
|
opts.requireOption('-i');
|
||||||
opts.requireOptionArgument('-i');
|
opts.requireOptionArgument('--input');
|
||||||
console.log('contains', opts.contains('-d'));
|
console.log('contains --debug', opts.contains('-d'));
|
||||||
|
console.log('contains --input', opts.contains('--input'));
|
||||||
|
|
||||||
// const _defaultErrorMessages = {
|
// const _defaultErrorMessages = {
|
||||||
// missingRequiredOption: (optionName) => `Missing required option '${optionName}'`,
|
// missingRequiredOption: (optionName) => `Missing required option '${optionName}'`,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue