Forum: Ruby simple optparse question on available options

Posted by Panagiotis Atmatzidis (Guest)
on 2012-07-24 21:19
(Received via mailing list)
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:

[...]

# http://ruby.about.com/od/advancedruby/a/optionparser2.htm
  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 Atmatzidis
Posted by Robert Klemme (robert_k78)
on 2012-07-24 22:58
(Received via mailing list)
On Tue, Jul 24, 2012 at 9:18 PM, Panagiotis Atmatzidis
<ml@convalesco.org> 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. :-)

Kind regards

robert
Posted by unknown (Guest)
on 2012-07-25 15:51
(Received via mailing list)
Am 24.07.2012 21:18, schrieb Panagiotis Atmatzidis:
>     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
Posted by Panagiotis Atmatzidis (Guest)
on 2012-07-25 17:38
(Received via mailing list)
Hello,

Thanks for the reply Robert and Stomar :-)

On 25 Ιουλ 2012, at 16:50 , sto.mar@web.de wrote:

>
> --
> <https://github.com/stomar/>
>


Panagiotis Atmatzidis
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.