Optparse and required options

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.

Jim

···


Jim Menard, jimm@io.com, http://www.io.com/~jimm/
“Lead me not into temptation. I can find it myself.”
– Jeffrey Kaplan’s .sig, seen in rec.humor.oracle.d

Hi,

At Thu, 25 Mar 2004 00:54:28 +0900,
Jim Menard wrote in [ruby-talk:95739]:

This forces the ‘–email’ option to take an argument. What I want to do is
force the user to supply an ‘–email EMAIL’ option.

optparse itself doesn’t supply options semantics.

Do I have to append

usage() if email.nil?

and if so, how do I get the optarg obejct’s help message?

You have to write mandatory and/or exclusive condtions by
yourself.

email or abort ARGV.options.to_s

···


Nobu Nakada

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 }

required:

  opt.on('-e', '--email=EMAIL', EMAIL address') { | e | email = e }

optional:

  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.

Jim

-a

···

On 24 Mar 2004, Jim Menard wrote:

===============================================================================

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================

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

···

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.