optparse and required arguments

Discussion in 'Ruby' started by E F van de Laar, Jan 17, 2004.

  1. Hi,

    I'm playing around with optparse and ran into something I couldn't get
    working right off hand. I define two options; both of which require an
    argument. I'm using a rescue clause "catch all" to catch options which
    do not get an argument passed in. If I pass two of these on the command
    line the second option gets passed as an argument to the first. Anyone
    have an idea on how to get around this?

    The test script and output...

    emiel@marvin:~/co/tally> cat test.rb
    require 'optparse'

    ARGV.options do |opts|
    opts.on("-L=LIB", String) { |arg| puts "-L=" +arg }
    opts.on("-R=LIB", String) { |arg| puts "-R=" +arg }
    begin
    opts.parse!
    rescue
    $stderr.puts "Error: #{$!}\n\n" + opts.to_s
    exit 1
    end
    end

    % ruby1.8 test.rb -L -R quux
    -L=-R

    % ruby1.8 test.rb -L foo -R
    -L=foo

    Cheers,

    Emiel
    --
    E F van de Laar
    www.il.fontys.nl/~emiel
    PGP pubkey: %finger
     
    E F van de Laar, Jan 17, 2004
    #1
    1. Advertising

  2. E F van de Laar

    Guest

    Hi,

    At Sat, 17 Jan 2004 09:12:14 +0900,
    E F van de Laar wrote:
    > I'm playing around with optparse and ran into something I couldn't get
    > working right off hand. I define two options; both of which require an
    > argument. I'm using a rescue clause "catch all" to catch options which
    > do not get an argument passed in. If I pass two of these on the command
    > line the second option gets passed as an argument to the first. Anyone
    > have an idea on how to get around this?


    If you want -L to get an argument only when next string doesn't
    seem an option, use "-L [LIB]".

    > require 'optparse'
    >
    > ARGV.options do |opts|

    opts.on("-L [LIB]", String) { |arg| puts "-L=#{arg.inspect}" }
    opts.on("-R [LIB]", String) { |arg| puts "-R=#{arg.inspect}" }
    > begin
    > opts.parse!
    > rescue
    > $stderr.puts "Error: #{$!}\n\n" + opts.to_s
    > exit 1
    > end
    > end
    >
    > % ruby1.8 test.rb -L -R quux

    -L=nil
    -R="quux"

    --
    Nobu Nakada
     
    , Jan 17, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tomi Silander
    Replies:
    3
    Views:
    911
    Grant Edwards
    Apr 6, 2005
  2. Alex Gittens
    Replies:
    1
    Views:
    460
    Peter Hansen
    Jul 10, 2005
  3. Steven Bethard
    Replies:
    0
    Views:
    397
    Steven Bethard
    May 10, 2006
  4. Roger Nordqvist

    optparse and required switches

    Roger Nordqvist, Oct 8, 2007, in forum: Ruby
    Replies:
    5
    Views:
    224
    Roger Nordqvist
    Oct 9, 2007
  5. Ben Woodcroft
    Replies:
    2
    Views:
    167
    Ben Woodcroft
    May 20, 2009
Loading...

Share This Page