Skip to content

Commit

Permalink
feat: remove setPlaceholderKeys (#1105)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
pvdlg authored and bcoe committed Jun 26, 2018
1 parent 26080ba commit 6ee2c82
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
5 changes: 2 additions & 3 deletions test/yargs.js
Expand Up @@ -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', () => {
Expand All @@ -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]')
})

Expand Down
17 changes: 2 additions & 15 deletions yargs.js
Expand Up @@ -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.
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -1103,7 +1100,7 @@ function Yargs (processArgs, cwd, parentRequire) {

self.exit(0)
})
return setPlaceholderKeys(argv)
return argv
}

// Handle 'help' and 'version' options
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand Down

0 comments on commit 6ee2c82

Please sign in to comment.