From c3166ef17b9882d017665476147ccaf0999e7717 Mon Sep 17 00:00:00 2001 From: n loewen Date: Sat, 23 Sep 2023 19:09:16 -0700 Subject: [PATCH] Change parse() output to always use long flag names as keys --- opter.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/opter.js b/opter.js index e739eac..692ae51 100644 --- a/opter.js +++ b/opter.js @@ -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; } }