Alex Rivera | Logout

How do you specify a required switch (not argument) with Ruby OptionParser?

Asked 2009-10-09T00:38:28.457
53

I'm writing a script and I want to require a --host switch with value, but if the --host switch isn't specified, I want the option parsing to fail.

I can't seem to figure out how to do that. The docs seem to only specify how to make the argument value mandatory, not the switch itself.

Edit
Report

1 Answer

1

The answer from unknown (google) is good, but contains a minor error.

rescue OptionParser::InvalidArgument, OptionParser::MissingArgument

should be

OptionParser::InvalidOption, OptionParser::MissingArgument

Otherwise, optparse.parse! will trigger the standard error output for OptionParser::InvalidOption, not the custom message.

answered 2010-02-04T08:19:07.683

Your Answer