On Jun 15, 2012, at 6:11 PM, "cyber c." <lists / ruby-forum.com> 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