From a42ea26c5abba8f5276b443e245d4b5d42cc9f63 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Tue, 26 Apr 2022 12:51:41 -0700 Subject: [PATCH 1/2] fix: dont clobber description for multiple option calls --- lib/yargs-factory.ts | 6 +++++- test/usage.cjs | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts index 279d8bcce..091142635 100644 --- a/lib/yargs-factory.ts +++ b/lib/yargs-factory.ts @@ -1044,7 +1044,11 @@ export class YargsInstance { } const desc = opt.describe || opt.description || opt.desc; - this.describe(key, desc); + const existingDesc = this.#usage.getDescriptions()[key]; + if (!existingDesc || typeof desc === 'string') { + this.describe(key, desc); + } + if (opt.hidden) { this.hide(key); } diff --git a/test/usage.cjs b/test/usage.cjs index becd9b125..8ea5d6309 100644 --- a/test/usage.cjs +++ b/test/usage.cjs @@ -3968,7 +3968,6 @@ describe('usage tests', () => { .strict() .parse() ); - console.log(r); r.errors[0] .split('\n') .should.deep.equal([ @@ -4838,4 +4837,23 @@ describe('usage tests', () => { ' -v, --version Custom version description [boolean]', ]); }); + + // https://github.com/yargs/yargs/issues/2169 + it('allows multiple option calls to not clobber description', () => { + const r = checkUsage(() => + yargs('--help') + .options({ + arg: {desc: 'Old description', type: 'string', default: 'old'}, + }) + .options({arg: {default: 'new'}}) + .wrap(null) + .parse() + ); + r.logs[0] + .split('\n') + .slice(-1)[0] + .replace(/\s+/g, ' ') + .trim() + .should.equal('--arg Old description [string] [default: "new"]'); + }); }); From 4deb0c62a58fbb15a64f7f46020580ccba46ed5d Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Wed, 18 May 2022 14:26:46 -0700 Subject: [PATCH 2/2] fixup! fix: dont clobber description for multiple option calls --- lib/yargs-factory.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts index 90ec72081..b54286c7c 100644 --- a/lib/yargs-factory.ts +++ b/lib/yargs-factory.ts @@ -1068,8 +1068,11 @@ export class YargsInstance { } const desc = opt.describe || opt.description || opt.desc; - const existingDesc = this.#usage.getDescriptions()[key]; - if (!existingDesc || typeof desc === 'string') { + const descriptions = this.#usage.getDescriptions(); + if ( + !Object.prototype.hasOwnProperty.call(descriptions, key) || + typeof desc === 'string' + ) { this.describe(key, desc); }