80 lines
3.3 KiB
JavaScript
80 lines
3.3 KiB
JavaScript
const Opter = require('./opter.js');
|
|
|
|
// const args = argparser.parse(process.argv);
|
|
// console.log('args', args);
|
|
// console.log('args', args);
|
|
// requireOption(args, '--i');
|
|
// argsrequire(args, '-i');
|
|
|
|
const opts = new Opter(process.argv)
|
|
opts.synonymize('-i', '--input');
|
|
opts.synonymize('-d', '--debug');
|
|
opts.requireOption('-i');
|
|
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}'`,
|
|
// missingRequiredArgument: (optionName) => `Missing argument for option ${optionName}`,
|
|
// tooManyArguments: (optionName) => `Too many arguments for option ${optionName}`,
|
|
// }
|
|
//
|
|
// /**
|
|
// * Declare multiple option flags to be synonymous.
|
|
// * Use this if you have short and long names for the same flag
|
|
// * eg: '-d' and '--debug'
|
|
// *
|
|
// * @param {Array<string>} namesWithDashes
|
|
// * @returns {void}
|
|
// **/
|
|
// function synonymize(opts /* <-- TODO tmp */, ...namesWithDashes) {
|
|
// namesWithDashes.slice(1).forEach((n) => {
|
|
// const x = _stripDashes(n);
|
|
// this.opts[x] = opts[namesWithDashes[0]]; // TODO check 'this' works
|
|
// });
|
|
// }
|
|
//
|
|
// function _stripDashes(optionName) {
|
|
// if (optionName.startsWith('--')) return optionName.substring(2);
|
|
// if (optionName.startsWith('-')) return optionName.substring(1);
|
|
// return optionName;
|
|
// }
|
|
//
|
|
// /**
|
|
// * Verify the presence of a required option
|
|
// * @param {string} nameWithDashes
|
|
// * @param {string} [errorMessage]
|
|
// * @returns {Boolean} **True** indicates that the option is present
|
|
// **/
|
|
// function requireOption(userProvidedOpts, nameWithDashes, errorMessage=null) {
|
|
// if (_stripDashes(nameWithDashes) in userProvidedOpts) return true;
|
|
// throw new Error(errorMessage || _defaultErrorMessages.missingRequiredOption(nameWithDashes));
|
|
// }
|
|
//
|
|
// /**
|
|
// * Verify the presence of arguments for an option that requires them,
|
|
// * if that option is present
|
|
// * @param {string} nameWithDashes
|
|
// * @param {number} [minRequired]
|
|
// * @param {number} [maxRequired]
|
|
// * @param {string} [tooFewMessage] Error message if there are too few arguments provided
|
|
// * @param {string} [tooManyMessage] Error message if there are too many arguments provided
|
|
// * @returns {Boolean} **True** indicates that the arguments are present, or that the option is *not* present
|
|
// **/
|
|
// // TODO remove first param when making this a method on an Args object
|
|
// function argsrequire(userProvidedArgs, nameWithDashes, minRequired=1, maxRequired=1, tooFewMessage=null, tooManyMessage=null) {
|
|
// let name = _stripDashes(nameWithDashes);
|
|
// if (name in userProvidedArgs) {
|
|
// console.log('type', typeof userProvidedArgs[name] === 'boolean');
|
|
// console.log(userProvidedArgs[name]);
|
|
// if (typeof userProvidedArgs[name] === 'boolean') {
|
|
// throw new Error(tooFewMessage || _defaultErrorMessages.missingRequiredArgument(nameWithDashes));
|
|
// } else if (userProvidedArgs[name].length < minRequired) {
|
|
// throw new Error(tooFewMessage || _defaultErrorMessages.missingRequiredArgument(nameWithDashes));
|
|
// } else if (userProvidedArgs[name].length > maxRequired) {
|
|
// throw new Error(tooManyMessage || _defaultErrorMessages.tooManyArguments(nameWithDashes));
|
|
// }
|
|
// }
|
|
// return true;
|
|
// }
|