-
-
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
Object default values display JSON under help menu #1283
Comments
Interesting idea. Would work nicely when there is a custom On a somewhat related note, there was an issue opened about whether a custom coercion function should be called on the default value rather than the default value being in final form. (A work-around is to handle the default in your own code when the option is undefined.) |
@shadowspawn I'm assuming you are referring to #1036. I feel the supplied default should be previously coerced, since it may hard to represent all desired default values directly from the command line. I'm actually not sure Either way, I don't see a real simple solution. My work around was this: function parseIP(value: string): IPv4 | string {
return IPv4.fromDecimalDottedString(value);
}
program.option('--dns <ip>', 'Additional DNS server to check', parseIP, '1.1.1.1');
...
if(typeof(program.dns) === 'string') {
program.dns = parseIP(program.dns);
} |
I finally found the comment where someone expected the default value to be processed by the coercion function : #400 (comment) |
@shadowspawn Thank you. Looking into this more, I thought that coercing the default value would be the best, but it appears it is more complicated. The coercion function also takes a Ultimately from the test cases, the default value should be previously coerced. The easiest would be to just add some more complex "stringify" functionality. Maybe something like this: switch(typeof(option.defaultValue)) {
case 'undefined': return '';
case 'string':
case 'boolean':
case 'number':
return JSON.stringify(option.defaultValue);
default /*object, etc*/: return option.defaultValue.toString();
} Thoughts? |
(The An additional check could be to only use |
(Reproducing a comment from discussion in #1309.) I see yargs handles this by having a
with a nice example of the function flavour:
This is a simple direct approach to the help problem and separates the value from the help representation. There is not anywhere currently to add properties like this, but I have been wondering about how to make options more extensible for the less common things: #518 #1106 |
Experimenting with Option approach in #1331: program
.addOption(new Option('-t, --timeout <delay>').default(60, 'one minute')) |
@shadowspawn that sounds wonderful, I will take a look at #1331. |
Included in v7.0.0 pre-release #1386 |
Included in Commander 7.0.0. |
Using an any object as the default value displays a JSON version of that object. This might not be very user friendly.
Here is an example using
IPv4
fromip-num
package:Consider changing
JSON.stringify
at https://github.com/tj/commander.js/blob/master/index.js#L1413 to a scheme utilizing.toString
instead. The impact could be pretty severe though.The text was updated successfully, but these errors were encountered: