Skip to content

Commit 6ee2c82

Browse files
pvdlgbcoe
authored andcommitted
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`
1 parent 26080ba commit 6ee2c82

2 files changed

Lines changed: 4 additions & 18 deletions

File tree

test/yargs.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ describe('yargs dsl tests', () => {
4040
argv.cat.should.eql(33)
4141
})
4242

43-
it('populates argv with placeholder keys for all options', () => {
43+
it('do not populates argv with placeholder keys for unset options', () => {
4444
const argv = yargs([])
4545
.option('cool', {})
4646
.parse()
4747

48-
Object.keys(argv).should.include('cool')
48+
Object.keys(argv).should.not.include('cool')
4949
})
5050

5151
it('accepts an object for implies', () => {
@@ -70,7 +70,6 @@ describe('yargs dsl tests', () => {
7070
)
7171

7272
r.errors[0].should.match(/really cool key/)
73-
r.result.should.have.property('x')
7473
r.result.should.not.have.property('[object Object]')
7574
})
7675

yargs.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,6 @@ function Yargs (processArgs, cwd, parentRequire) {
10531053
for (let i = (commandIndex || 0), cmd; argv._[i] !== undefined; i++) {
10541054
cmd = String(argv._[i])
10551055
if (~handlerKeys.indexOf(cmd) && cmd !== completionCommand) {
1056-
setPlaceholderKeys(argv)
10571056
// commands are executed using a recursive algorithm that executes
10581057
// the deepest command first; we keep track of the position in the
10591058
// argv._ array that is currently being executed.
@@ -1066,7 +1065,6 @@ function Yargs (processArgs, cwd, parentRequire) {
10661065

10671066
// run the default command, if defined
10681067
if (command.hasDefaultCommand() && !skipDefaultCommand) {
1069-
setPlaceholderKeys(argv)
10701068
return command.runCommand(null, self, parsed)
10711069
}
10721070

@@ -1084,7 +1082,6 @@ function Yargs (processArgs, cwd, parentRequire) {
10841082
self.exit(0)
10851083
}
10861084
} else if (command.hasDefaultCommand() && !skipDefaultCommand) {
1087-
setPlaceholderKeys(argv)
10881085
return command.runCommand(null, self, parsed)
10891086
}
10901087

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

11041101
self.exit(0)
11051102
})
1106-
return setPlaceholderKeys(argv)
1103+
return argv
11071104
}
11081105

11091106
// Handle 'help' and 'version' options
@@ -1147,7 +1144,7 @@ function Yargs (processArgs, cwd, parentRequire) {
11471144
else throw err
11481145
}
11491146

1150-
return setPlaceholderKeys(argv)
1147+
return argv
11511148
}
11521149

11531150
self._runValidation = function runValidation (argv, aliases, positionalMap, parseErrors) {
@@ -1173,16 +1170,6 @@ function Yargs (processArgs, cwd, parentRequire) {
11731170
}
11741171
}
11751172

1176-
function setPlaceholderKeys (argv) {
1177-
Object.keys(options.key).forEach((key) => {
1178-
// don't set placeholder keys for dot
1179-
// notation options 'foo.bar'.
1180-
if (~key.indexOf('.')) return
1181-
if (typeof argv[key] === 'undefined') argv[key] = undefined
1182-
})
1183-
return argv
1184-
}
1185-
11861173
// an app should almost always have --version and --help,
11871174
// if you *really* want to disable this use .help(false)/.version(false).
11881175
self.help()

0 commit comments

Comments
 (0)