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

yargs doesn't enforce strictOptions for options passed without = #483

Closed
acolytec3 opened this issue Sep 18, 2023 · 6 comments
Closed

yargs doesn't enforce strictOptions for options passed without = #483

acolytec3 opened this issue Sep 18, 2023 · 6 comments

Comments

@acolytec3
Copy link

I'm not sure if this is user error or a bug in yargs but my team has recently observed that where we have .strictOptions set, yargs doesn't error when we run our CLI app with an option passed like this:
node index.js --unknownOption unknownValue

though it does error when we run it as:
node index.js --unknownOption=unknownValue.

However, when we run node index.js --knownOption acceptableValue, the behavior is the same as if we ran it with node index.js --knownOption=acceptableValue.

Here is the actual issue we're tracking on our codebase if it's helpful.

Is my expectation incorrect that an option passed with --option=value should be parsed the same way as --option value?

@shadowspawn
Copy link
Member

I was not able to reproduce this behaviour in a small test program. Both forms shows the help with an error Unknown argument: unknownOption.

In the linked issue, it mentions "unknown CLI params" and "unknown commands" and "unknown keyword arguments are being caught as expected but positional ones are not". To be clear, is the observed problem with an "option" starting with a - or --, or perhaps a non-option argument type?

@acolytec3
Copy link
Author

It's with what y'all call "options". Sorry for the lack of clarity. I'm wondering where things go awry because when I took just our yargs definition that starts here and copy it into a new script and try to run with the noted --datadir (from our issue), the unknownOption error appears. It's just when we run it with our compiled Typescript code where it doesn't show up.

@shadowspawn
Copy link
Member

I managed to reproduce your problem using your tsconfig.json. The problem is the legacy yargs export does some tricky things, and TypeScript does some tricky things, and they don't always go together.

You can confirm by calling your broken program with unknown option and --help, and see if ----unknownOption appears in the help [sic].

node index.js --unknownOption unknownValue --help

See yargs/yargs#2010 (comment) for some research, which does not cover the settings you are using.

As a work-around, you could use a special TypeScript import style that has worked with all the tsconfig settings I have tried:

// import yargs from 'yargs'
import yargs = require("yargs");

See: https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require

@acolytec3
Copy link
Author

Thank you for the guidance! I will research #2010 further and see if any of those options could work for us as I don't think the import = require... will work for us with our current build setup. Will circle back here and close things out once I find a solution.

@acolytec3
Copy link
Author

Just to round this out in case anybody has a similar issue. For us, the solution was just to change our import for yargs from a default import of the sort:
import yargs from 'yargs'
to a namespace import:
import * as yargs from 'yargs'.

Thanks again for the help! Great project and we just added yargs autocomplete to our CLI tool as a bonus (since I didn't know it existed before).

@shadowspawn
Copy link
Member

(Thanks for circling back.)

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

No branches or pull requests

2 participants