I’ve just started trying out ruby, having been using perl, and
encountered a small problem with the getopt module, as follows.
I re-wrote a script that is meant to glob huge numbers of files,
to get around the shell’s globbing limitations. It has options
for the suffix to glob “-g” and other things to do e.g. “-d” to
delete them. However, the options work in one order but not another,
e.g.
$ ./globber.rb -g gbk -d
globs *.gbk and deletes the files.
$ ./globber.rb -d -g gbk
Fails to glob any files and exits.
Here are the relevant parts of the code.
parser = GetoptLong.new
parser.ordering = GetoptLong::PERMUTE
parser.set_options(
["-h", “–help”, GetoptLong::NO_ARGUMENT],
["-d", “–delete”, GetoptLong::NO_ARGUMENT],
["-g", “–glob”, GetoptLong::REQUIRED_ARGUMENT],
["-o", “–output”, GetoptLong::OPTIONAL_ARGUMENT]
)
process options
files = nil
outfile = nil
loop do
begin
opt, arg = parser.get
break if not opt
case opt
when “-g"
files = Dir[”*.#{arg}"]
when "-d"
files.each {|f|
File.unlink(f)
puts “Deleting #{f}…”
}
exit
end
end
if files == nil
puts "No files available!"
else
puts files
end
Presumably my problem is the loop, but I can’t see any other way to do
this in the documents. Is there an equivalent of perl’s use of a hash
to process options?
Thanks for any suggestion.
···
–
Remove surname to reply.
http://www.bioinf.ceh.ac.uk/lab/