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

demand() ignores anything after -- #1732

Closed
cspotcode opened this issue Sep 2, 2020 · 6 comments · Fixed by #1752
Closed

demand() ignores anything after -- #1732

cspotcode opened this issue Sep 2, 2020 · 6 comments · Fixed by #1752
Labels

Comments

@cspotcode
Copy link
Contributor

Here's an example:
https://runkit.com/cspotcode/5f4fe1f3c5d0c8001a637b54

some-cli --arrayOption foo bar -- positional1 positional2

With:

.demand(2)
.array('arrayOption')

Complains: "Not enough non-option arguments: got 0, need at least 2"

An array option accepts multiple values. So to separate it from the demanded positionals, we use --.
demand() ignores all values after --. Removing demand() shows that the values are -- are parsed correctly and included in the _ array.

Does demand's deprecation mean this cannot be fixed?

@bcoe
Copy link
Member

bcoe commented Sep 10, 2020

@cspotcode I conceptually think of keys after -- as meaning, "this is no longer parsing, please stop applying parsing rules and validation to them"

I feel like this is really a problem with implicit arrays, which are a feature I don't love; since they create ambiguity between positionals and array arguments.

If I could do it over, I think I'd suggest writing the above more like this:

some-cli --arrayOption foo --arrayOption bar positional1 positional2

Where each member of the array is explicit. And -- is only used in the cases where you need to actually break out of the parse.

@bcoe
Copy link
Member

bcoe commented Sep 10, 2020

There's a setting called "greedy arrays" which I'd like to eventually consider defaulting to false:

https://github.com/yargs/yargs-parser#greedy-arrays

@cspotcode
Copy link
Contributor Author

These comments aren't addressing the bug raised by this ticket.

@bcoe
Copy link
Member

bcoe commented Sep 10, 2020

@cspotcode I suppose what I'm getting at is that, is that I feel this potentially falls in the category of relying on undocumented behavior, and I don't really want to enshrine the behavior, if I can convince you not to use the array type like this.

@cspotcode
Copy link
Contributor Author

cspotcode commented Sep 10, 2020

It affects YousefED/typescript-json-schema#362, so it's not actually my code that's using arrays.

The behavior also crops up without using arrays, so it's pretty clearly a bug.

cli-that-processes-files -- -annoyingfilenamecomingfromsomewherewedonotcontrol-.txt outputfile.txt

The filename could also be the same as a flag or option, and then you'd want -- to separate positionals.

EDIT: it could also be clockclitakesweirdargs -1minute +2days, stuff like that.

@bcoe
Copy link
Member

bcoe commented Sep 10, 2020

@cspotcode so the argument is that, if you're collecting positionals after -- in the _ variable; they should count towards the demand(N) quota?

Is the behavior the same for demandCommand of curiosity?

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

Successfully merging a pull request may close this issue.

2 participants