From: botp Date: 2012-05-16T14:23:29+09:00 Subject: Re: Option Parser with an optional flag/switch for last argument On Wed, May 16, 2012 at 5:56 AM, Neubyr Neubyr wrote: > Thanks for the reply everyone. I am taking approach as suggested by > Bartosz and Chad to assign 'left-over' ARGV to the input decimal number. > > @Mike, the commandable gem looks really useful. I am not using it right > now, but it looks like it will be useful in some of my other > projects/command-line apps. Thanks for providing a detailed example. if you need flexibility, then ARGV by itself is more than enough. eg, re your sample, $ cat -n test2.rb 1 2 operator=ARGV.shift 3 4 ops=%w(* / + -) 5 6 if not (ops).include? operator 7 raise "operator argument must be one of #{ops}" 8 end 9 10 factors=ARGV 11 if factors.size < 2 12 raise "need at least two args for operator" 13 end 14 15 factors=ARGV.map(&:to_f) 16 17 p factors.inject(operator.to_sym) 18 19 botp@u:~ $ ruby test2.rb - 3 4 5 #subtract -6.0 botp@u:~ $ ruby test2.rb + 3 4 5 #add 12.0 botp@u:~ $ ruby test2.rb "*" 3 4 5 #multiply; need to quote 60.0 just a thought. kind regards -botp