yargs / yargs/yargs-parser

coerce and 'number' typed options

Open
#347 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
519
Forks
129
Avg merge
1h 34m
Merged PRs (30d)
1

Description

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?"

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the timestamp option with type set to "number" and inspect yargs-parser's processValue and applyCoercions flow, as identified in the issue. Determine whether coercion should receive the original value or the pre-parsed value, then document or correct the behavior so the intended type and coercion contract is explicit and verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.