Add 'contains' function
This commit is contained in:
parent
c2ea099bbf
commit
b9e15019c0
10
opter.js
10
opter.js
|
|
@ -15,6 +15,7 @@ const defaultErrorMessages = {
|
|||
tooManyArguments: (optionName) => `Too many arguments for option ${optionName}`,
|
||||
}
|
||||
|
||||
// TODO figure out how to avoid having to use '<options>.opts to access the output of this'
|
||||
|
||||
module.exports = class Opts {
|
||||
constructor(argv) {
|
||||
|
|
@ -73,6 +74,7 @@ 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]);
|
||||
|
|
@ -86,6 +88,14 @@ module.exports = class Opts {
|
|||
}
|
||||
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;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
3
test.js
3
test.js
|
|
@ -8,9 +8,10 @@ const Opter = require('./opter.js');
|
|||
|
||||
const opts = new Opter(process.argv)
|
||||
opts.synonymize('-i', '--input');
|
||||
opts.synonymize('-d', '--debug');
|
||||
opts.requireOption('-i');
|
||||
opts.requireOptionArgument('-i');
|
||||
console.log(opts.opts);
|
||||
console.log('contains', opts.contains('-d'));
|
||||
|
||||
// const _defaultErrorMessages = {
|
||||
// missingRequiredOption: (optionName) => `Missing required option '${optionName}'`,
|
||||
|
|
|
|||
Loading…
Reference in New Issue