From: Bryan Richardson Date: 2008-02-25T12:22:56+09:00 Subject: Re: OptionParser - no short options or incomplete options ------=_Part_6886_16441256.1203909781572 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Thanks again 7stud. Your suggestions have answered my ultimate question, which is "is it possible to have OptionParser only parse on fully matching options." The answer obviously seems to be "no." I'll be implementing your 2nd suggestion. -- Bryan On Sun, Feb 24, 2008 at 8:00 PM, 7stud -- wrote: > Bryan Richardson wrote: > > I'd just hate for someone to fat-finger a simple option and end up > > deleting > > an entire database worth of data. Am I overlooking something? Also, > > what > > if I wanted -l to do one thing, and --long_option to do something > > different? I'm not trying to get on anyone's nerves here, I'm just > > wondering if forcing long options is doable... > > > > -- > > Thanks! > > Bryan > > Personally, I think all the optparse modules in any language are more > trouble than they are worth. Invariably, you can examine ARGV yourself > and come up with a solution quicker than trying to figure out how the > overly complex optparse modules work. > > In particular, the ruby optparse module appears to have a design flaw: > it doesn't send the option that was actually entered to the on() method. > > > 1) This seems to work: > > require 'optparse' > > opts = OptionParser.new do |opts| > opts.on('--long_option') do |val| > #destroy stuff > puts "in first on" > end > > opts.on('--long_optio') do |val| > #do nothing > puts "in 2nd on" > end > end > > opts.parse! > > > 2) Or, you can always do this: > > require 'optparse' > > puts 'start:' > puts ARGV > > prohibited = ['-l', '--l'] > > ARGV.delete_if do |str_in_argv| > prohibited.include?(str_in_argv) > end > > puts 'end:' > puts ARGV > > opts = OptionParser.new do |opts| > opts.on('--long_option') do |val| > #destroy stuff > puts "This won't output for -l or --l short form." > end > end > > opts.parse! > > > > > > Also, what > > if I wanted -l to do one thing, and --long_option to > > do something different? > > > > require 'optparse' > > opts = OptionParser.new do |opts| > opts.on('--long_option') do |val| > puts 'in first on' > end > > opts.on('-l') do |val| > puts 'in 2nd on' > end > end > > opts.parse! > > > > -- > Posted via http://www.ruby-forum.com/. > > ------=_Part_6886_16441256.1203909781572--