# Optparser question

**URL:** <https://www.ruby-forum.com/t/optparser-question/190902>\
**Category:** Ruby\
**Created:** [June 30, 2010, 1:27pm UTC](https://www.ruby-forum.com/t/optparser-question/190902 "2010-06-30T13:27:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [June 30, 2010, 1:27pm UTC](https://www.ruby-forum.com/t/optparser-question/190902/1 "2010-06-30T13:27:32Z")

</div>

I am using optparser to parse arguments.How can I set a mandatory  
option? If user doesn’t specify this option, parse() method should throw  
an error message.

thanks,

Zhao Yi

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [June 30, 2010, 2:35pm UTC](https://www.ruby-forum.com/t/optparser-question/190902/2 "2010-06-30T14:35:07Z")

</div>

2010/6/30 Zhao Yi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am using optparser to parse arguments.How can I set a mandatory  
> option? If user doesn’t specify this option, parse() method should throw  
> an error message.

There are no mandatory options - after all, they are _options_. You  
can only have mandatory arguments to options:

[http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html](http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html)

You need an extra flag which you set when the option is seen. Then you  
can throw or do whatever after #parse has finished.

Kind regards

robert
