# GetoptLong usage

**URL:** <https://www.ruby-forum.com/t/getoptlong-usage/195812>\
**Category:** Ruby\
**Created:** [September 30, 2010, 4:20pm UTC](https://www.ruby-forum.com/t/getoptlong-usage/195812 "2010-09-30T16:20:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [September 30, 2010, 4:20pm UTC](https://www.ruby-forum.com/t/getoptlong-usage/195812/1 "2010-09-30T16:20:55Z")

</div>

Hi,

I’m using Example: [Learn How to Blog and Build Websites for Profit!](http://rubylearning.com/satishtalim/ruby_arrays.html)  
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,  
Richard

# SummarizeBalances.rb

# K:\_Projects\Ruby\_Ruby\_Apps\InvestmentSW

# Example: [Learn How to Blog and Build Websites for Profit!](http://rubylearning.com/satishtalim/ruby_arrays.html)

require ‘GetoptLong’

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

opts = GetoptLong.new(

```

[“–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

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [September 30, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/getoptlong-usage/195812/2 "2010-09-30T16:40:14Z")

</div>

On Thursday 30 September 2010, RichardOnRails wrote:

> |Thanks in Advance,  
> | opts = GetoptLong.new(  
> | 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  
> ruby  
> standard library) rather than GetoptLong, since it provides a much more  
> ruby-  
> oriented interface.

Stefano

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [September 30, 2010, 5:30pm UTC](https://www.ruby-forum.com/t/getoptlong-usage/195812/3 "2010-09-30T17:30:35Z")

</div>

On Sep 30, 10:38 am, Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

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 🙂

> > OptionParser  
> > Looks good, especially the comprehensive example.

Best wishes,  
Richard
