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

package:args shouldn't throw ArgumentError on missing argument #872

Open
eseidel opened this issue Mar 19, 2025 · 2 comments
Open

package:args shouldn't throw ArgumentError on missing argument #872

eseidel opened this issue Mar 19, 2025 · 2 comments

Comments

@eseidel
Copy link

eseidel commented Mar 19, 2025

ArgumentError is an Error and thus supposed to be used for programmer errors.

ArgResults.operator[] throws ArgumentError on what should be a runtime error:

throw ArgumentError('Could not find an option named "--$name".');

Am I wrong?

@RandalSchwartz
Copy link

I could argue that Error subclasses are for things that the programmer should have prevented through proper coding but failed to do so. But this error seems to be correct code and a problem with the data. I could easily argue that this should be an API Exception rather than a programmer Error.

@lrhn
Copy link
Member

lrhn commented Mar 20, 2025

You're wrong 😉
At least on that line. Now, line 74 on the other hand ...

In line 69 it checks if the name the programmer passes in is one that the same programmer has defined in the parser options for the parser. If there is no defined option with that name, then it's not a name of an option at all.
The programmer can be expected to know the names of all options that the argument parser was configured with.

In line 74, an ArgumentError is thrown for a data issue

if (option.mandatory && !_parsed.containsKey(name)) {
throw ArgumentError('Option $name is mandatory.');

The option was defined, as required even, but the parsed result contains no data for it.
There are two possibilities here:

  • This can't happen, parsing would have thrown instead (which would be The Way!), but then it shouldn't be throwing an ArgumentError, just have an assert or throw an AssertionError.
  • It can happen, and then it should throw an Exception (and be documented as doing so) or have a different return value (also documented).

Issue title checks out, example off by 5 lines. I think that's close enough!

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

No branches or pull requests

3 participants