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

Conversation

davidyuk
Copy link

@davidyuk davidyuk commented Apr 7, 2024

Pull Request

Problem

I have an option that gets passed to the underlying library. I want to describe its default behaviour, but I can't pass any default value there except undefined. In case undefined the default value description is not printed in help.

const { program, Option } = require('commander');

const ttl = new Option('--ttl [ttl]', 'Transaction can be mined before provided height')
  .default(undefined, 'current height increased by 3');

program.addOption(ttl);
program.parse(process.argv);

Output:

$ node ./test.js --help
Usage: test [options]

Options:
  --ttl [ttl]  Transaction can be mined before provided height
  -h, --help   display help for command

Solution

I'm proposing to print defaultValueDescription even if it is provided without defaultValue. Though I'm not sure that it is not a misuse of "default value" concept.

Expected output:

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

Options:
  --ttl [ttl]  Transaction can be mined before provided height (default: current height increased by 3)
  -h, --help   display help for command

ChangeLog

Added: show defaultValueDescription even if defaultValue is not set

This PR is supported by the Æternity Foundation

@shadowspawn
Copy link
Collaborator

Nice tidy PR.

It does feel enough like a misuse of "default value" concept that I am reluctant to add this.

... I can't pass any default value there except undefined.

Are you able to put in a placeholder default value (like false or null or even a symbol) and fix it up before passing to the underlying library? I appreciate this means you need to add an extra line of code in your code to do the work, so not free.


For example:

const { program, Option } = require('commander');

const ttl = new Option('--ttl [ttl]', 'Transaction can be mined before provided height')
  .default(false, 'current height increased by 3');

program.addOption(ttl);
program.parse(process.argv);
if (program.opts().ttl === false) program.setOptionValue('ttl', undefined); // replace placeholder value
console.log(program.opts());
% node index.js --help
Usage: index [options]

Options:
  --ttl [ttl]  Transaction can be mined before provided height (default: current height increased by 3)
  -h, --help   display help for command
% node index.js       
{ ttl: undefined }

@davidyuk
Copy link
Author

davidyuk commented Apr 8, 2024

This workaround worked in my case, thank you! You may close this PR

@shadowspawn
Copy link
Collaborator

Thanks for update @davidyuk

@shadowspawn shadowspawn closed this Apr 8, 2024
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