Change parse() output to always use long flag names as keys

This commit is contained in:
n loewen 2023-09-23 19:09:16 -07:00
parent f43915c735
commit c3166ef17b
1 changed files with 8 additions and 1 deletions

View File

@ -40,6 +40,9 @@ module.exports = class Opter {
parse(argv) {
this.userOptions = argvToObject(argv);
// collect user opts with the long names as keys, for output
let normalizedUserOptions = {};
Object.keys(this.definedOptions).forEach(longName => {
let shortName = this.synonyms[longName];
let reqs = this.definedOptions[longName];
@ -52,6 +55,10 @@ module.exports = class Opter {
if (typeof this.userOptions[longName] === 'object') { userArgs = this.userOptions[longName]; }
if (typeof this.userOptions[shortName] === 'object') { userArgs = this.userOptions[shortName]; }
if (provided) {
normalizedUserOptions[longName] = typeof userArgs === 'object' ? userArgs : true;
}
/* Check that required options are provided */
if (reqs.required && !provided) {
throw new Error(`Missing required option '${longName}'`);
@ -70,7 +77,7 @@ module.exports = class Opter {
}
})
return this.userOptions;
return normalizedUserOptions;
}
}