From: Brian Candler Date: 2011-03-03T07:23:56+09:00 Subject: Re: array of valid arguments in optparse Ritesh Agrawal wrote in post #985009: > I am trying to use optparse to parse an argument into an array and check > for possible values. For instance consider an argument like this > > --operations count, count+, count- > > I want to parse above argument into @options[:operations] = ["count", > "count+", "count-"]. Well, the spaces will cause the shell to split it; the user will probably need to type --operations count,count+,count- or --operations "count, count+, count-" If you look at the source code for optparse.rb you'll find an example of list arguments: # # List of arguments. # opts.on("--list x,y,z", Array, "Example 'list' of arguments") do |list| # options.list = list # end > But at the same time I have limited vocabulary for > possible operations and I want to check for that so that if some gives > any other operation argument, it throws an error message Just throw the error yourself. OPERATIONS = ["count", "count+", "count-"] opts.on ... do |hypo| raise OptionParser::InvalidArgument, "Invalid operation" unless (hypo - OPERATIONS).empty? end -- Posted via http://www.ruby-forum.com/.