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 and 'number' typed options #347

Open
ankon opened this issue Jan 21, 2021 · 2 comments
Open

coerce and 'number' typed options #347

ankon opened this issue Jan 21, 2021 · 2 comments
Labels

Comments

@ankon
Copy link

ankon commented Jan 21, 2021

I have an option definition like this:

	'timestamp': {
		description: 'Timestamp to use (ISO 8601 formats)',
		type: 'number',
		coerce(value: any) {
			const ts = moment(value, moment.ISO_8601, true);
			if (!ts.isValid()) {
				process.exitCode = 1;
				throw new Error(`Cannot parse provided timestamp ${value}`);
			}
			return ts.valueOf();
		},
	},

Somehow this started to break "recently", and complains about the value inside coerce being NaN, when the value provided to the option is something like this: 2021-01-21 17:25:47.
ISTR that this used to work in processing the value correctly, but looking at the sources I see that yargs-parser's processValue is executed before applyCoercions.

Now, I can get the behavior I need by changing the type to string, but before doing that: Is this "intentional", i.e. is the type intended to mean "type before it gets to coerce", rather than "type after it has been coerced?"

@ankon
Copy link
Author

ankon commented Jan 21, 2021

Somehow this started to break "recently"

I just tested with various yargs versions, and I have to take that back: This never seems to have worked.

Test script was broken, after fixing it the change seems to have appeared between yargs 14.0.0 and yargs 14.1.0. "Recently" might be a problem somewhere else -- my library depends on yargs@13, but I see that npm found a way to install yargs@15 in the project where things break.

@shadowspawn
Copy link
Member

A heads-up that there were big changes to coerce handling in Yargs v17 to support async. Yargs no longer gets yargs-parser to do the coercion.

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

No branches or pull requests

3 participants