From: Gavin Sinclair Date: 2004-03-25T05:51:24+09:00 Subject: Re: optparse and required options On Thursday, March 25, 2004, 2:54:28 AM, Jim wrote: > I'm just starting to use optparse. I want to force the user to supply an > option. How can I do this with optparse? Here's what I have > email = nil > ARGV.options { | opt | > opt.on('-e', '--email EMAIL', EMAIL address') { | e | email = e } > opt.parse! > } > This forces the '--email' option to take an argument. What I want to do is > force the user to supply an '--email EMAIL' option. > Do I have to append > usage() if email.nil? > and if so, how do I get the optarg obejct's help message? > Thanks for your help. Here are my idioms: def show_help(parser, code=0, io=STDOUT) io.puts parser exit(code) end o = options = OpenStruct.new parser = OptionParser.new do |p| # ... p.on('-e', '--email ADDRESS', 'Send email to ADDRESS') do |a| o.operation = :email o.address = a end # ... p.on('-h', '--help', 'Show this message') do show_help(p) end end parser.parse!(ARGV) if o.operation.nil? show_help(parser, 1, STDERR) end Hope this helps, Gavin