Thursday, 15 July 2010

How to require one of two possible options in a Ruby script? -



How to require one of two possible options in a Ruby script? -

i'm trying configure couple options in ruby script using optionparser. i'd them both optional require @ to the lowest degree 1 used. 1 alternative allow single value passed command line. other alternative tell script utilize file containing multiple values iterated over.

this i've been able come with:

options = {} parser = optionparser.new |opts| opts.banner = "usage: sat_server_delete.rb [options]" opts.on('-f', '--file file', 'file') |file| options[:file] = file; end opts.on('-s', '--server server', 'server') |server| options[:server] = server end end.parse!

i haven't worked out logic each utilize yet. right i'd happy see error indicating neither have been provided. far i'm not getting that. script runs completion , returns prompt.

i looked @ ruby doc optionparser, isn't clear me how accomplish i'm trying. possible have either/or situation options? i'm not sure if have makes sense. i'm attempting re-create logic without understanding doing.

you can check options hash after parse! in

if options[:file] && options[:server] puts "may have 1 of -f , -s" exit elsif !options[:file] && !options[:server] puts "must have @ to the lowest degree 1 of -f , -s" exit end

ruby options

No comments:

Post a Comment