From f91d9b334ad9cfce79a89c08ff210c622b7c528f Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Mon, 18 Jul 2022 11:11:37 -0700 Subject: [PATCH] fix: dont clobber description for multiple option calls (#2171) --- lib/yargs-factory.ts | 9 ++++++++- test/usage.cjs | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts index 60c61266e..c093dbc36 100644 --- a/lib/yargs-factory.ts +++ b/lib/yargs-factory.ts @@ -1044,7 +1044,14 @@ export class YargsInstance { } const desc = opt.describe || opt.description || opt.desc; - this.describe(key, desc); + const descriptions = this.#usage.getDescriptions(); + if ( + !Object.prototype.hasOwnProperty.call(descriptions, key) || + typeof desc === 'string' + ) { + this.describe(key, desc); + } + if (opt.hidden) { this.hide(key); } diff --git a/test/usage.cjs b/test/usage.cjs index 31138889d..d80770879 100644 --- a/test/usage.cjs +++ b/test/usage.cjs @@ -4846,4 +4846,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"]'); + }); });