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

'coerce' does not execute for defaults #598

Closed
nyurik opened this issue Aug 17, 2016 · 7 comments
Closed

'coerce' does not execute for defaults #598

nyurik opened this issue Aug 17, 2016 · 7 comments

Comments

@nyurik
Copy link

nyurik commented Aug 17, 2016

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.

@nyurik
Copy link
Author

nyurik commented Aug 17, 2016

Hmm, it seems like it does work, my mistake. Will submit a documentation PR.

@nyurik nyurik closed this as completed Aug 17, 2016
@satazor
Copy link
Member

satazor commented Oct 7, 2016

@nyurik I've the same issue.. the coerce function is not called for the default value. How did you make it work?

@bcoe
Copy link
Member

bcoe commented Oct 7, 2016

@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.

@satazor
Copy link
Member

satazor commented Oct 7, 2016

@bcoe I am on yargs v6.

// 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);

screen shot 2016-10-07 at 19 54 00

@satazor
Copy link
Member

satazor commented Oct 7, 2016

I'm currently using .check() to do what I want to do, though it doesn't feel right to mutate stuff in it.

@bcoe bcoe added the bug label Oct 7, 2016
@bcoe bcoe reopened this Oct 7, 2016
@bcoe
Copy link
Member

bcoe commented Oct 7, 2016

@satazor 👍 will dig into this.

@ssonal
Copy link
Contributor

ssonal commented Oct 22, 2016

@bcoe I looked into this; seems the problem is with yargs-parser. Specifically, coercions are applied before defaults are set into argv. Happy to submit a pull-request that helps fix the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants