GetoptLong usage

R

RichardOnRails

Hi,

I'm using Example: 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: 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
 
S

Stefano Crocco

|Hi,
|
|I'm using Example: 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: 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

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
 
R

RichardOnRails

|Hi,
|
|I'm using Example: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: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

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

Hi Stefano,

Excellent response.
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 :)
Looks good, especially the comprehensive example.

Best wishes,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top