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?
Yes, just use the ARGV as if you would without using OptionParser;
once it has parsed everything it recognizes, it will leave the rest of
arguments in ARGV.
On Wed, May 16, 2012 at 12:03:07AM +0900, Neubyr N. 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?
You could just access it directly in ARGV after parsing everything else
using OptionParser.
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.
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.