Hallo,
ich möchte gerne in eine Kommandozeilenoption erzwingen. Hier mein
Code:
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: me [options] file1 file2 ..."
opts.on( '-v', '--verbose', 'Output more information' ) do |v|
options[:verbose] = v
end
options[:outdir] = nil
opts.on( '-o', '--outdir DIR', 'Write output to DIR' ) do |dir|
if dir.nil? or dir.empty? then
puts "--outdir must have an argument"
puts opts
exit
end
puts "dir #{dir}"
options[:outdir] = dir
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end.parse!
if options[:outdir].nil? or options[:outdir].empty? then
puts "--outdir must have an argument"
puts opts
exit
end
Dabei ist gewünscht, dass eine --optdir option auf jeden Fall
angegeben werden muss. Die Fehlerbehandlung innerhalb des
OptionParser.new-Blocks greift nur, wenn --outdir= angegeben ist, also
wirklich --outdir mit ohne Parameter angegeben wurde. Und in der
Fehlerbehandlung außerhalb des Blocks funktioniert das "puts opts"
nicht.
Wie kann ich beides haben, also die Fehlerbehandlung innerhalb des
OptionParser.new-Blocks und eine Fehlermeldung werfen, wenn --outdir
überhaupt nicht angegeben ist?
Grüße
Marc
--
-------------------------------------- !! No courtesy copies, please !! -----
Marc Haber | " Questions are the | Mailadresse im Header
Mannheim, Germany | Beginning of Wisdom " |
http://www.zugschlus.de/
Nordisch by Nature | Lt. Worf, TNG "Rightful Heir" | Fon: *49 621 72739834