First of all in my tests the errors are only triggered when calling .each
, not in the constructor so this:
require "getoptlong"
GetoptLong.new(
[ "--argument", "-a", GetoptLong::REQUIRED_ARGUMENT ]
)
# ~$ ruby script.rb --argument
Doesn’t show any error.
This shows the error:
require "getoptlong"
GetoptLong.new(
[ "--argument", "-a", GetoptLong::REQUIRED_ARGUMENT ]
).each
# ~$ ruby script.rb --argument
Error:
test_getoptlong.rb: option `--argument' requires an argument
/Users/me/.rbenv/versions/3.1.1/lib/ruby/3.1.0/getoptlong.rb:398:in `set_error': option `--argument' requires an argument (GetoptLong::MissingArgument)
If I don’t pass the argument --argument
at all, I don’t see any error:
require "getoptlong"
GetoptLong.new(
[ "--argument", "-a", GetoptLong::REQUIRED_ARGUMENT ]
).each
# ~$ ruby script.rb
No error.
As this is a GetoptLong::REQUIRED_ARGUMENT
, I expected to see an error if the user didn’t add it to the script call.
How can I raise an error if any GetoptLong::REQUIRED_ARGUMENT
is missing?