GetoptLong order of options

M

Milo Thurston

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.
 
R

Robert Klemme

Milo Thurston said:
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?

Your problem is that you do the work while you are processing options. If
you place -d before -g then you won't have globbed anything when
encountering -d - so there's nothing to delete. Normally you first
process options and then act on them. This is what I did when using
GetoptLong (untested):

require 'getoptlong'

files = []
outfile = nil
delete = nil

opts = GetoptLong.new(
["--help", "-h", GetoptLong::NO_ARGUMENT],
["--delete", "-d", GetoptLong::NO_ARGUMENT],
["--glob", "-g", GetoptLong::REQUIRED_ARGUMENT],
["--output", "-o", GetoptLong::OPTIONAL_ARGUMENT]
)

opts.each do |opt, arg|
case opt
when "--help"
puts "Help!"
when "--delete"
delete = true
when "--glob"
files.concat Dir["*.#{arg}"]
when "--output"
outfile = arg
else
raise "Error"
end
end

if files.empty?
$stderr.puts "no files"
else
files.each do |f|
puts "Deleting #{f}"
File.unlink(f)
end if delete

puts files
end

Note: now that optparse is part of the std distribution you might want to
look into that, too.

Regards

robert
 
M

Milo Thurston

Robert Klemme said:
This is what I did when using
GetoptLong (untested):

Thanks, I'll try that out.
Note: now that optparse is part of the std distribution you might want to
look into that, too.

Is there any on-line documentation for it? I had some trouble trying to
find the getoptlong info.
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Command Line Arguments 0
getoptlong question 5
About GetoptLong and exceptions 2
GetoptLong#.quiet not doing what expected 2
hash 6
rescue block doesn't get run 3
hash 13
printing from a file - beginner 0

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top