Simple optparse question on available options

Hello,

I’m using optparse for a cli script. I need an argument (-s, --sort) to
accept only 3 possible options :date, :country, :ipfreq, so here is my
piece of optparse code:

[…]

Use OptionParser to Parse Commands in Ruby

options[:sort] = :yes # <— Not sure what this does, probably sets a
default value to :sort … I just grab the code sample from the link
above

opts.on(‘-s’, ‘–sort OPT’, [:date, :country, :ip], ‘Sorts by [date,
country, ip-frequency]’) do |sort|
options[:sort] = sort
end

[…]

Now I don’t understand how to use this code in my exec function:

[…]
if File.exists?($logfile)
$f2b = F2bread.new($logfile)
if options[:info]
$f2b.info
# opts.on(‘-s’, ‘–sort OPT’, [:date, :country, :ip], ‘Sorts by
[date, country, ip-frequency]’) do |sort|
elsif options[:sort]
if options[:sort] == ‘date’
$f2b.sort_by_date
elsif options[:sort] == “country” # <— something like this?
# etc…
[…]

Any sort of hints are always welcome.

Best regards,

Panagiotis A.

On Tue, Jul 24, 2012 at 9:18 PM, Panagiotis A.
[email protected] wrote:

opts.on(‘-s’, ‘–sort OPT’, [:date, :country, :ip], ‘Sorts by [date, country,
ip-frequency]’) do |sort|
options[:sort] = sort
end

[…]
# opts.on(‘-s’, ‘–sort OPT’, [:date, :country, :ip], ‘Sorts by [date,
country, ip-frequency]’) do |sort|
elsif options[:sort]
if options[:sort] == ‘date’
$f2b.sort_by_date
elsif options[:sort] == “country” # <— something like this?
# etc…
[…]

Any sort of hints are always welcome.

$ irb19 -r optparse
irb(main):001:0> OptionParser.new {|o| o.on(“-s O”, [:a, :b]){|v| p v,
v.class}}.parse %w{-s a}
:a
Symbol
=> []

Nice pun, btw. :slight_smile:

Kind regards

robert

Am 24.07.2012 21:18, schrieb Panagiotis A.:

if options[:sort] == 'date'
  $f2b.sort_by_date
elsif options[:sort] == "country" # <--- something like this?
  # etc...

you are dealing with symbols, so

if options[:sort] == :date

Hello,

Thanks for the reply Robert and Stomar :slight_smile:

On 25 Ιουλ 2012, at 16:50 , [email protected] wrote:


https://github.com/stomar/

Panagiotis A.