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

[Bug] Unexpected arguments ignored #2149

Open
KaKi87 opened this issue Feb 8, 2024 · 3 comments
Open

[Bug] Unexpected arguments ignored #2149

KaKi87 opened this issue Feb 8, 2024 · 3 comments
Labels
pending release Merged into a branch for a future release, but not released yet

Comments

@KaKi87
Copy link

KaKi87 commented Feb 8, 2024

Hi,

When passing more arguments to a command than it expects, the command is executed without error, receiving only the expected arguments.

It would be nice to throw an error instead, preventing potantially wrong actions/results unforeseen by the user.

One particular case is when an argument is a name/title/label, in which the user may include spaces while forgetting quotes.

On the other hand, I wonder if it would be possible, when said argument is the last (or only) one, to optionally put everything automatically in it, kinda like the spread operator in JS (but still outputting a string) ?

Thanks

@shadowspawn
Copy link
Collaborator

It would be nice to throw an error instead, preventing potantially wrong actions/results unforeseen by the user.

You can make this an error by calling .allowExcessArguments(false).

On the other hand, I wonder if it would be possible, when said argument is the last (or only) one, to optionally put everything automatically in it, kinda like the spread operator in JS (but still outputting a string) ?

You can explicitly collect the remaining arguments by declaring the last one as variadic like `.argument('[more-args...]').

Also, all of the arguments are available using cmd.args. This includes both the declared and the excess arguments.

@KaKi87
Copy link
Author

KaKi87 commented Feb 9, 2024

Oh, I'm sorry I didn't find these beforehand. 😅

allowExcessArguments(false)

I'd like to request this to be default though, for security reasons.

Thanks

@shadowspawn
Copy link
Collaborator

I originally intended excess arguments to be an error by default, but made it opt-in for the first release to reduce breakage at the time:

On a related note, there was an in-depth discussion during the development of parseArgs about unexpected positionals and we eventually decided to make positionals opt-in for strict mode (a shout-out to the thoughtful and patient championing by @bakkot). The extra step in parseArgs to allow positionals has not caused any user feedback that I am aware of.

I am leaning towards making allowExcessArguments false by default in next major version of Commander.

@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending release Merged into a branch for a future release, but not released yet
Projects
None yet
Development

No branches or pull requests

2 participants