From: Robert Klemme Date: 2013-03-01T22:02:55+09:00 Subject: Re: Is it possible to use OptionParser with short options such as -oo? On Fri, Mar 1, 2013 at 1:45 PM, Shareef J. wrote: > I'm trying to wrap a command line tool with a Ruby script in order to be > able to clean up and validate any user supplied arguments before calling > the tool. I'm therefore trying to use OptionParser to parse the command > line arguments and pass them on to the tool after processing. > > However, the tool accepts short arguments that have multi character > names. I think this is self contradictory: short options are one character options. :-) > For example, '-o filename' and '-oo filename' where the double > 'o' signifies that the file should be overwritten and not appended to. > OptionParser can't cope with multi char short options so what can I do > here? Can I extend OptionParser in any way or should I try and manually > parse for these short options before calling option parser? Doesn't > seem too clean that way. That doesn't seem to fit the model of OptionParser well. You would have to define that -o takes an optional argument but if you do that it'll use the second "o" as argument. irb(main):017:0> op = OptionParser.new do |opts| irb(main):018:1* opts.on('-o=[arg]') {|v| o += 1; o_val << v if v} irb(main):019:1> end => Usage: irb [options] -o=[arg] irb(main):020:0> op.parse %w{-o} => [] irb(main):021:0> o => 1 irb(main):022:0> o_val => [] irb(main):023:0> o = 0; o_val = [] => [] irb(main):024:0> op.parse %w{-o val} => ["val"] irb(main):025:0> o => 1 irb(main):026:0> o_val => [] irb(main):027:0> o = 0; o_val = [] => [] irb(main):028:0> op.parse %w{-oo val} => ["val"] irb(main):029:0> o => 1 irb(main):030:0> o_val => ["o"] If the tool has an option --dry-run or similar I'd simply add that to the command line and see what it reports. Other than that I'd probably avoid the validation. Reason is that you need to change your validation every time the tool's API changes - and you have the validation code in two places. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/