-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Support using a function to get default value for option and arg #1036
Comments
Work-arounds: For options, you could prompt for missing values after the parse. For non-option arguments, a missing required argument is intended to generate an error (although there are some holes in the error detection!). For some cases you might be able to mark the argument as optional rather than required and prompt for missing values after the parse. (There is a long thread discussing mandatory arguments including options in #230.) |
That won't work with the existing call patterns. The default value is used as a starting value for custom option processing: |
I think there's a bug (if it's not a bug, then it's perfect for my proposed solution about
Example: const program = require('commander');
function myParseInt(value, dummyPrevious) {
console.log('Inside myParseInt', value, dummyPrevious);
return parseInt(value);
}
program.option('-i, --integer <number>', 'integer argument', myParseInt, 123);
program.parse(process.argv);
console.log(program.integer); $ node myprogram.js
123
$ node myprogram.js -i 456
Inside myParseInt 456 123
456 |
Please let me explain the motivation behind this feature request: For a good user experience, when we design a command like this:
We want to communicate with users that: When users do not specify arg1 and option1, displaying prompts (using Inquirer) is just a fallback so that users have a better user experience. It still communicates well with users that they need to specify arg1 and option1. |
This is as intended The custom option processing function is only called when the option is specified on the command line. |
Thank you for the nice explanation. (My suggested work-around was not a solution, just an idea for offering prompt without changing Commander.) |
I'm thinking about a workaround by wrapping Use When
is called, call
to parse the option ( Then override
so that when help for the option is displayed, users will see
Also set
When Inside For command args, do something similarly. This workaround is very ugly, so it's best to enhance Commander.js to support using a function to get default value for option and arg.
We tune the design like this: What do you think? |
I think it may be too complicated to add this behaviour for a special case. I am painfully aware that took years to catch up with adding a parameter to turn the coercion function into a more general purpose custom option processing. This feels a bit similar. More specifically though, I want to add support for mandatory options before trying to add support for prompting for their value. (#230) |
Early similar issue: #4 |
On a related note, I noticed yargs has a concept of middleware for additional processing of arguments before they are processed, with an example of reading option values from a file if not specified: |
This issue has not had any activity in a few months. It isn't likely to get acted on due to this report. Feel free to open a new issue if it comes up again, with new information and renewed interest. Thank you for your contributions. |
I have a use case like this:
If a required option or arg is not given, fall back to using Inquirer.js to prompt users to input.
The current signature of
#option
:One solution to my use case is to change it to this:
When
defaultValueOrFn
is a function, it will return aPromise
containing the default value.Similarly for
#parseExpectedArgs
(and related methods).Is there a workaround, without having to change Commander.js?
If there's no workaround, I'll create a PR.
The text was updated successfully, but these errors were encountered: