diff --git a/lib/yargs-parser.ts b/lib/yargs-parser.ts index 0b29c9aa..111100e5 100644 --- a/lib/yargs-parser.ts +++ b/lib/yargs-parser.ts @@ -414,9 +414,26 @@ export class YargsParser { }) if (configuration['camel-case-expansion'] && configuration['strip-dashed']) { - Object.keys(argv).filter(key => key !== '--' && key.includes('-')).forEach(key => { - delete argv[key] - }) + if (configuration['dot-notation']) { + Object.keys(flags.aliases) + .filter(key => key !== '--' && key.includes('-')) + .map(key => key.split('.')) + .forEach(keyPath => { + var obj = argv + for (const subkey of keyPath) { + if (subkey.includes('-')) { + delete obj[subkey] + break + } else { + obj = obj[subkey] + } + } + }) + } else { + Object.keys(argv).filter(key => key !== '--' && key.includes('-')).forEach(key => { + delete argv[key] + }) + } } if (configuration['strip-aliased']) { diff --git a/test/yargs-parser.cjs b/test/yargs-parser.cjs index 159e1dc7..76c23bc7 100644 --- a/test/yargs-parser.cjs +++ b/test/yargs-parser.cjs @@ -3636,6 +3636,32 @@ describe('yargs-parser', function () { 'test-value': 1 }) }) + + // See: https://github.com/yargs/yargs-parser/issues/223 + it('strip-dashed removes subkeys made by dot-notation', function () { + var argv = parser(['--dashed-will-be-stripped', '--here.it-will-too', + '--foo.not-dashed.also-not-dashed', '--foo.definitley-not-dashed'], { + configuration: { + 'strip-dashed': true + } + }) + argv.should.not.have.property('dashed-will-be-stripped') + argv.here.should.not.have.property('it-will-too') + argv.here.should.have.property('itWillToo') + argv.foo.should.not.have.property('not-dashed') + argv.foo.notDashed.should.not.have.property('also-not-dashed') + argv.foo.notDashed.should.have.property('alsoNotDashed') + }) + + it('strip-dashed removes dotted keys with dot-notation disabled', function () { + var argv = parser(['--foo.not-dashed'], { + configuration: { + 'strip-dashed': true, + 'dot-notation': false + } + }) + argv.should.not.have.property('foo.not-dashed') + }) }) describe('prototype collisions', () => {