Skip to content

"Coerce" calls always on option, does it correct behaviour? #2158

@lis355

Description

@lis355

Run this simple script WITHOUT any cli arguments:

const path = require("path");

const yargs = require("yargs/yargs");

const argv = yargs(process.argv.slice(2))
	.option("someArg", {
		type: "string",
		coerce: path.resolve
	})
	.parse();

You will get error message in output:

[C:\Program]() Files\nodejs\node.exe [.\main.js]()
Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]
  --someArg                                                             [string]
node_modules/yargs/build/index.cjs:1
The "path" argument must be of type string. Received undefined
node_modules/yargs/build/index.cjs:1
Process exited with code 1

Does it correct? If my someArg is not a demand, why he "gets" default value undefined and then coerce is called? And, of cource, path.resolve drop this error

I think, it's not correct, to call coerce, if user not give an argument

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions