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
'coerce' does not execute for defaults #598
Comments
Hmm, it seems like it does work, my mistake. Will submit a documentation PR. |
@nyurik I've the same issue.. the coerce function is not called for the default value. How did you make it work? |
@satazor mind providing some code that reproduces; make sure you're on the most up-to-date yargs (we bumped a major) because we now apply coercion slightly differently. |
@bcoe I am on yargs // coerce.js
#!/usr/bin/env node
'use strict';
const yargs = require('yargs');
const moment = require('moment');
const argv = yargs
.strict()
.option('min-commits', {
type: 'string',
alias: 'mc',
default: '10:6M',
describe: 'Filter results based on a minimum number of commits within a certain time-frame (format is <count>:<duration>, see http://momentjs.com/docs/#/durations/creating/ for the duration format)',
})
.coerce('min-commits', (arg) => {
const split = arg.split(':');
const count = Number(split[0]);
const durationValue = parseInt(split[1], 10);
const durationUnit = split[1].match(/([a-z]*)$/i)[1];
if (isNaN(count)) {
throw new Error('Invalid count specified for min-commits, format is <count>:<duration>');
}
if (isNaN(durationValue) || !moment.normalizeUnits(durationUnit)) {
throw new Error('Invalid date when calculating the date for min-commits, format is <count>:<duration>');
}
return { count, date: moment().subtract(durationValue, durationUnit).toDate() };
})
.argv;
console.log('min-commits', argv.minCommits); |
I'm currently using |
@satazor 👍 will dig into this. |
@bcoe I looked into this; seems the problem is with yargs-parser. Specifically, coercions are applied before defaults are set into |
There is no way to make
coerce
execute for the default value, yet there are some usecases where this would be very useful. Without this, coerce cannot be used, and argument has to be processed after parsing.The text was updated successfully, but these errors were encountered: