and change or remove the "-i", "--inplace [EXTENSION]" option for something else than i:
Now, the -i will still match, but the other option "-F", "--irs [OCTAL]"!
In a more complete stack this resulted in a hard to find error. Also, to fix this (and raise the required error) you need to check the ARGV directly which renders optparse a bit less useful.
require 'optparse'
require 'pp'
class OptparseExample
def self.parse(args)
options = {}
opt_parser = OptionParser.new do |opts|
opts.on("-F", "--irs [OCTAL]", OptionParser::OctalInteger,
"Specify record separator (default \\0)") do |rs|
options["trigger"] = "I got triggered"
end
end
opt_parser.parse!(args)
options
end # parse()
end # class OptparseExample
options = OptparseExample.parse(ARGV)
pp options
pp ARGV
Try executing this script. The only difference between the previous script and this one is that instead of returning the options variable, opt_parser.parse!(args) is being returned from the method. I did not get the error with this script.
require 'optparse'
require 'pp'
class OptparseExample
def self.parse(args)
options = {}
opt_parser = OptionParser.new do |opts|
opts.on("-F", "--irs [OCTAL]", OptionParser::OctalInteger,
"Specify record separator (default \\0)") do |rs|
options["trigger"] = "I got triggered"
end
end
opt_parser.parse!(args)
end # parse()
end # class OptparseExample
options = OptparseExample.parse(ARGV)
pp options
pp ARGV
This is what OptParser doing by default. If your code were opts.on("--irs [OCTAL]", OptionParser::OctalInteger,, i.e. without the '-F', you would get '--irs' as well as shorthand '-i' available. Your case is bit specific, that the short and long parameters are quite different.
If you override the '-i' option somewhere and give it different meaning, the OptParser will handle it correctly.
If you really want to avoid the default short version of '-i', I can't see anything easier then fire the OptionParser::InvalidOption in the '-i' handler.
I'm not sure I would consider this a bug, but I can definitely see it as an undesired feature in some cases. I think it would be worthwhile to add a way to turn off this feature, and require that options specified on the command line match exactly. Attached is a patch that adds a require_exact accessor that does this.