On Tue, May 15, 2012 at 5:03 PM, Neubyr Neubyr <[email protected]> wrote:
> I am using OptionParser to parse command-line options. I would like to
> run a program in following manner:
>
> $ multiply-by.rb --factor 3 4
> $ 12
>
> where last number '4' is an input argument which should not need any
> option flag/switch. Any hints on how can it be done? Is it possible with
> OptionParser or should I write my own option parser method?

This is how I'd do it:

require 'optparse'

factor = nil

OptionParser.new do |opts|
  opts.on '-f', '--factor=F', Integer, 'Multiplication factor' do |v|
    factor = v
  end
end.parse! ARGV

abort "ERROR: need a factor" unless factor

ARGV.each {|v| puts(Integer(v) * factor)}

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to