-
Notifications
You must be signed in to change notification settings - Fork 147
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
Add support for command-specific yargs-parser option specs #464
base: master
Are you sure you want to change the base?
Conversation
I like the idea in principle! I would like to see if it causes problems with nontrivial code bases like AWS Amplify CLI, Solidarity, and the upcoming Ignite CLI. |
Agreed. The tests are non-exhaustive and I've only tried this with https://github.com/graphprotocol/graph-cli. My feeling is that unless there is a global CLI spec built from all commands and their options, there will always be a level of ambiguity. Since I didn't want to fork gluegun just for this, I've implemented a simple heuristic in an upcoming feature for graph-cli to deal with the original problem: let { withX } = toolbox.parameters.options
let array = fixParameters(toolbox.parameters, { booleanOptions: { withX } }) where Still, if a command like Potential solutions / improvements:
|
After thinking this over, I agree that doing it the way you explain in your original PR sounds the best. I'd love if you could bring this to a point where you feel it's testable, and then I'll go test it with Ignite and Solidarity. @Jannis thanks for your work! |
Is there a way to get something like this supported where we can define option types and have them auto-documented? |
@danawoodman Left a response on the other PR: #427 (comment) |
This allows any command to specify how its options are parsed, based on the opts argument for yargs-parser:
Without this, invokations like
would result in
arg1
being consumed as the value for--with-x
, andtoolbox.parameters.first
being set toarg2
. To me this is undesirable, because it really complicates handling input arguments in any non-trivial command.The new
options
feature allows commands to control how their arguments are parsed, making it possible forto parse to
{ withX: true, 'with-x': true }
options, withtoolbox.parameters.first
beingarg1
andtoolbox.parameters.second
beingarg2
.The hard part of this change is detecting commands correctly without having parsed the options first. This PR is a draft because the way I implemented this is maybe not elegant and robust enough. Basically, it scans all arguments (including options) and builds two separate arrays, one for the command path, the other for all the unrelated arguments. When encountering a
-foo
or--foo
argument, it pushes it to the unrelated arguments array. The next value is then treated as a command path segment only if[...prefix, nextValue]
is a valid command path. It is treated as an option value when encountering situations like--option cmd cmd
however.@skellock @jamonholmgren Curious to hear what you think!