Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show defaultValueDescription even if defaultValue is not set #2183

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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