Skip to content

Commit

Permalink
Show defaultValueDescription even if defaultValue is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 7, 2024
1 parent b95ea44 commit 22664a5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/help.js
Expand Up @@ -302,7 +302,10 @@ class Help {
`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(', ')}`,
);
}
if (option.defaultValue !== undefined) {
if (
option.defaultValue !== undefined ||
option.defaultValueDescription !== undefined
) {
// default for boolean and negated more for programmer than end user,
// but show true/false for boolean option as may be for hand-rolled env or config processing.
const showDefault =
Expand Down Expand Up @@ -344,7 +347,10 @@ class Help {
`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(', ')}`,
);
}
if (argument.defaultValue !== undefined) {
if (
argument.defaultValue !== undefined ||
argument.defaultValueDescription !== undefined
) {
extraInfo.push(
`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`,
);
Expand Down
13 changes: 13 additions & 0 deletions tests/help.optionDescription.test.js
Expand Up @@ -70,6 +70,19 @@ describe('optionDescription', () => {
);
});

test('when option has default value description without default value then return description and custom default description', () => {
const description = 'description';
const defaultValueDescription = 'custom';
const option = new commander.Option('-a <value>', description).default(
undefined,
defaultValueDescription,
);
const helper = new commander.Help();
expect(helper.optionDescription(option)).toEqual(
`description (default: ${defaultValueDescription})`,
);
});

test('when option has choices then return description and choices', () => {
const description = 'description';
const choices = ['one', 'two'];
Expand Down

0 comments on commit 22664a5

Please sign in to comment.