GetoptLong usage


I’m using Example:
as a guide to GetoptLong usage. I get the message:

uninitialized constant GetoptLong::OptionalArgument (NameError)

for line 10 of the program listed below (with line 10 flagged). Can
anyone point me to a working example of GetoptsLong usage?

Thanks in Advance,




require ‘GetoptLong’

puts "Args = " + ARGV.join(", ")

opts =

[ “–from”, “-f”, GetoptLong::OptionalArgument], # Line 10
[ “–to”, “-t”, GetoptLong::OptionalArgument],
[ “–weeks_until”, “-w”, GetoptLong::OptionalArgument]

# Process the arguments
opts.each { |opt, arg|

puts %{%s => “%s”} % [opt, arg]
rem_args = ARGV.join(", ")
puts “Remaining args = %s” % [rem_args] if rem_args

On Thursday 30 September 2010, RichardOnRails wrote:

|Thanks in Advance,
| opts =
| puts “Remaining args = %s” % [rem_args] if rem_args
As you can see from the link you posted, the constant name is
GetoptLong::OPTIONAL_ARGUMENT, not GetoptLong::OptionalArgument.
However, I’d
consider using OptionParser (which, like GetoptLong is included in the
standard library) rather than GetoptLong, since it provides a much more
oriented interface.


On Sep 30, 10:38 am, Stefano C. [email protected] wrote:

|require ‘GetoptLong’
| opts.each { |opt, arg|


Hi Stefano,

Excellent response.

As you can see …
Admittedly, I did see the syntax in the example. What I didn’t see,
alas, was the precise option I wanted to use. By time I clicked my
SciTE editor, thus overlaying the example, I forgot the syntax.
Remembering the words was all I could muster. Sad tale :slight_smile:

Looks good, especially the comprehensive example.

Best wishes,

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs