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
[bug]: Incompatibility between parser configuration and completion #2330
Comments
Exposing the parser configuration has made writing yargs code more complicated! The way this is handled in yargs-parser is by always looking for the option or its aliases and ignoring the configuration options. (This time it might be worth writing an internal convenience routine to make this easier.) Related: #2308 |
@shadowspawn if you can point me towards the desired solution I'm happy to implement, I'm not super happy with our current workaround |
I have not looked deep into this, so treat with a little scepticism. I assume the faulty code is:
Prior art is I wrote code to find the coerce keys like this, looking for the original key or any of its aliases in argv. For coerce I wanted to do something with each of the aliases so filtered for all the matches.
In the completion case we just want to know if the key is present, so instead of So I had in mind something like (typed direct into comment, not syntax checked!):
Does that make enough sense? Side note: why not test Because looking in |
(And thanks for the detailed original report, complete with motivations and suggested implementation and alternatives. Excellent.) |
PR opened 🎉, thanks @shadowspawn! |
Yargs Parser Configuration + Completion incompatibilities
Description
There is an incompatibility between the parser configuration options and the completion feature within yargs. Setting
strip-dashed: true
in the parser configuration causes the completion feature to fail.This is because the completion feature relies on passing '--get-yargs-completions' verbatim to yargs. Within the
Completion
class, thecompletionKey
is explicitly set toget-yargs-completions
. Within the YargsInstance, we explicitly check the following condition:completionKey in argv
, in this location:Since the argv has been parsed already at this point, the argument is now 'getYargsCompletions' instead of 'get-yargs-completions'. This causes the completion feature to never be called.
Reproduction
Steps to reproduce
node ./index.js --get-yargs-completions
and note that there is no output.index.js
by removing thestrip-dashed: true
option from the parser configuration.node ./index.js --get-yargs-completions
and note that the completion feature is called and the output is as expected.Workaround and Motivation
This issue was discovered when working on implementing completion for Nx. In our case, we have a small process which runs before yargs is bootstrapped, and preprocesses some arguments. We also happen to already export our parser configuration.
For us, we can check if
process.argv[2] === '--get-yargs-completions'
and removestrip-dashed: true
from our parser configurations. However, this is not a general solution, and it would be nice if the completion feature could be used in conjunction with the parser configuration.Suggested Implementation
The completionKey is fed through the parser transformations so that it matches any transformations that are applied to the arguments.
Alternate Implementations
completionKey
. This would let us, or others who notice a similar breakage, to override the key to match the transformations that are applied to the arguments.The text was updated successfully, but these errors were encountered: