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

Correctly display default value Infinity in optionHelp instead of null #1406

Closed
wants to merge 1 commit into from
Closed

Correctly display default value Infinity in optionHelp instead of null #1406

wants to merge 1 commit into from

Conversation

fnogatz
Copy link

@fnogatz fnogatz commented Dec 1, 2020

Pull Request

Problem

If an option has a default value of Infinity, it is printed as null in the help, because JSON.stringify(Infinity) === null.

Test program:

const { program } = require('commander');
program.option('-m, --max <int>', 'max value', parseInt, Infinity);
program.parse(process.argv);

Current output:

> ./test.js --help
Usage: test [options]

Options:
  -m, --max <int>  max value (default: null)
  -h, --help       display help for command

Solution

This PR adds a wrapper function stringify for the used JSON.stringify which checks for the values Infinity and -Infinity.

Corrected output:

> ./test.js --help
Usage: test [options]

Options:
  -m, --max <int>  max value (default: Infinity)
  -h, --help       display help for command

ChangeLog

Fixed: correctly print default value Infinity in option help text

@shadowspawn
Copy link
Collaborator

Good description, thanks.

Commander v7 will allow custom display of a default value, see #1331. This was prompted by discussion and investigation around improving certain classes where the JSON stringify was not as human readable as direct toString (#1283), quite like your case. The attempt to generalise it did not work out and instead added a custom solution to give user full control.

I think the new .default() should cover your use case?

@fnogatz
Copy link
Author

fnogatz commented Dec 1, 2020

I was not aware of the v7 branch 😇

However, if there is no defaultValueDescription set (via the second argument of .default()), it still uses JSON.stringify() internally. Theat means though my problem would be resolved by explicitly calling .default(Infinity, 'Infinity') in Commander v7, I would expect .default(Infinity) to handle this correctly. If you also think so, I could target my PR against the release/7.x branch instead, it's just the same change for the new optionDescription function.

@shadowspawn
Copy link
Collaborator

Yes, I wasn't suggesting Option.default() automatically supported Infinity, but does support supplying a custom description to be displayed as you saw. I don't want to add special case handling of unusual values to Commander itself.

Thank you for your contributions.

@shadowspawn shadowspawn closed this Dec 1, 2020
@fnogatz fnogatz deleted the option-help-infinity branch December 1, 2020 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants