Skip to content

Commit

Permalink
fix(coerce): options using coerce now displayed in help
Browse files Browse the repository at this point in the history
The trick we were using of .alias("foo", "foo") to "tell" yargs-parser about a key
using coerce has a bug such that help output is broken. We can instead use
.key (https://github.com/yargs/yargs-parser/blob/master/lib/yargs-parser.ts#L191) which
is designed for exactly our purposes (telling yargs-parser a key exists, without
setting attributes such as description).

Fixes #1909
  • Loading branch information
bcoe committed Apr 11, 2021
1 parent 747f7a9 commit 8d924fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/yargs-factory.ts
Expand Up @@ -368,7 +368,7 @@ export class YargsInstance {
// This noop tells yargs-parser about the existence of the option
// represented by "keys", so that it can apply camel case expansion
// if needed:
this.alias(keys, keys);
this.#options.key[keys] = true;
this.#globalMiddleware.addCoerceMiddleware(
(
argv: Arguments,
Expand Down
19 changes: 19 additions & 0 deletions test/yargs.cjs
Expand Up @@ -2176,6 +2176,25 @@ describe('yargs dsl tests', () => {
yargs().coerce('c');
}, /coerce callback must be provided/);
});

// Refs: https://github.com/yargs/yargs/issues/1909
it('shows coerced option in help', async () => {
const help = await yargs()
.option('option1', {
describe: 'option1 description',
type: 'string',
demandOption: true,
})
.option('option2', {
describe: 'option2 description',
type: 'string',
demandOption: true,
})
.coerce('option2', () => undefined)
.getHelp();
console.info(help);
help.should.match(/option2 description/);
});
});

describe('stop parsing', () => {
Expand Down

0 comments on commit 8d924fa

Please sign in to comment.