From: Eric Hodel Date: 2012-06-17T02:53:33+09:00 Subject: Re: Help using optionparser On Jun 15, 2012, at 6:11 PM, "cyber c." wrote: > I wanted to take cmd line inputs of the form > > File.rb -p A > File.rb -p B STRING > File.rb -p C STRING > > Im using optionparser to parse the arguments. > > I can create an arg and force it to accept inputs of A/B/C but how do i ensure the extra arg is accepted only when its B/C? > > opts.on("-p [STRING]", [:A,:B,:C], "") do |v| > options[:p] = v > end If it is positional, use Array instead of fixed values and check manually: opts.on "-p [STRING]", Array do |ary| raise OptionParser::InvalidArgument, "'A' cannot be followed by other values" # … end File.rb -p A,String # fails File.rb -p B,String If you use this type of input multiple times you can create a validator you can reuse, too