From 6ee2c82df515cb1b062e4135a5dd9c386fed2b21 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Tue, 26 Jun 2018 00:42:06 -0400 Subject: [PATCH] feat: remove `setPlaceholderKeys` (#1105) BREAKING CHANGE: Options absent from `argv` (not set via CLI argument) are now absent from the parsed result object rather tahn being set with `undefined` --- test/yargs.js | 5 ++--- yargs.js | 17 ++--------------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/test/yargs.js b/test/yargs.js index 1feb49309..9301922f3 100644 --- a/test/yargs.js +++ b/test/yargs.js @@ -40,12 +40,12 @@ describe('yargs dsl tests', () => { argv.cat.should.eql(33) }) - it('populates argv with placeholder keys for all options', () => { + it('do not populates argv with placeholder keys for unset options', () => { const argv = yargs([]) .option('cool', {}) .parse() - Object.keys(argv).should.include('cool') + Object.keys(argv).should.not.include('cool') }) it('accepts an object for implies', () => { @@ -70,7 +70,6 @@ describe('yargs dsl tests', () => { ) r.errors[0].should.match(/really cool key/) - r.result.should.have.property('x') r.result.should.not.have.property('[object Object]') }) diff --git a/yargs.js b/yargs.js index c2ac0fe98..683d5c393 100644 --- a/yargs.js +++ b/yargs.js @@ -1053,7 +1053,6 @@ function Yargs (processArgs, cwd, parentRequire) { for (let i = (commandIndex || 0), cmd; argv._[i] !== undefined; i++) { cmd = String(argv._[i]) if (~handlerKeys.indexOf(cmd) && cmd !== completionCommand) { - setPlaceholderKeys(argv) // commands are executed using a recursive algorithm that executes // the deepest command first; we keep track of the position in the // argv._ array that is currently being executed. @@ -1066,7 +1065,6 @@ function Yargs (processArgs, cwd, parentRequire) { // run the default command, if defined if (command.hasDefaultCommand() && !skipDefaultCommand) { - setPlaceholderKeys(argv) return command.runCommand(null, self, parsed) } @@ -1084,7 +1082,6 @@ function Yargs (processArgs, cwd, parentRequire) { self.exit(0) } } else if (command.hasDefaultCommand() && !skipDefaultCommand) { - setPlaceholderKeys(argv) return command.runCommand(null, self, parsed) } @@ -1103,7 +1100,7 @@ function Yargs (processArgs, cwd, parentRequire) { self.exit(0) }) - return setPlaceholderKeys(argv) + return argv } // Handle 'help' and 'version' options @@ -1147,7 +1144,7 @@ function Yargs (processArgs, cwd, parentRequire) { else throw err } - return setPlaceholderKeys(argv) + return argv } self._runValidation = function runValidation (argv, aliases, positionalMap, parseErrors) { @@ -1173,16 +1170,6 @@ function Yargs (processArgs, cwd, parentRequire) { } } - function setPlaceholderKeys (argv) { - Object.keys(options.key).forEach((key) => { - // don't set placeholder keys for dot - // notation options 'foo.bar'. - if (~key.indexOf('.')) return - if (typeof argv[key] === 'undefined') argv[key] = undefined - }) - return argv - } - // an app should almost always have --version and --help, // if you *really* want to disable this use .help(false)/.version(false). self.help()