optparse: Arguments as Arguments Unexpectedness

Discussion in 'Ruby' started by Ben Woodcroft, May 20, 2009.

  1. Hi,

    I am trying to use the standard optionparser to read in some arguments,
    but one of those arguments is an argument itself. Is this a bug?

    opt.rb:

    require 'optparse'
    OptionParser.new do |opts|
    opts.on('-a','--args [ARGUMENTS]',String,"arguments") {|v| puts "Found:
    #{v}"}
    end.parse!

    EXPECTED:
    $ ruby opt.rb -a "-d me"
    Found: -d me

    ACTUAL:
    $ ruby opt.rb -a "-d me"
    Found:
    /usr/lib/ruby/1.8/optparse.rb:1445:in `complete': invalid option: -d me
    (OptionParser::InvalidOption)
    from /usr/lib/ruby/1.8/optparse.rb:1443:in `catch'
    from /usr/lib/ruby/1.8/optparse.rb:1443:in `complete'
    from /usr/lib/ruby/1.8/optparse.rb:1282:in `parse_in_order'
    from /usr/lib/ruby/1.8/optparse.rb:1249:in `catch'
    from /usr/lib/ruby/1.8/optparse.rb:1249:in `parse_in_order'
    from /usr/lib/ruby/1.8/optparse.rb:1243:in `order!'
    from /usr/lib/ruby/1.8/optparse.rb:1334:in `permute!'
    from /usr/lib/ruby/1.8/optparse.rb:1355:in `parse!'
    from opt.rb:3
    --
    Posted via http://www.ruby-forum.com/.
    Ben Woodcroft, May 20, 2009
    #1
    1. Advertising

  2. Ben Woodcroft wrote:
    > I am trying to use the standard optionparser to read in some arguments,
    > but one of those arguments is an argument itself. Is this a bug?


    I don't think so. I think it's an ambiguity because you have declared
    your -a flag to take an *optional* argument.

    Suppose your OptionParser also honoured the -d option. It would need to
    choose between "-a" with argument "-d me", and "-a" with no argument
    followed by option "-d" with argument "me". OptionParser is taking the
    view that anything which starts with a dash in that case is intended to
    be the next option on the line. Otherwise, the argument to the -a flag
    would hardly be optional if it always consumed the next string.

    If your -a option *always* takes an argument, then declare it as such,
    without the square brackets:

    require 'optparse'
    OptionParser.new do |opts|
    opts.on('-a','--args ARGUMENTS',String,"arguments") {|v| puts "Found:
    #{v}"}
    end.parse!

    $ ruby opt.rb -a "-d me"
    Found:
    -d me

    Otherwise, your original code works as long as you include the flag and
    its argument in the same position.

    $ ruby opt.rb "-a-d me"
    Found:
    -d me
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, May 20, 2009
    #2
    1. Advertising

  3. Brian Candler wrote:
    > If your -a option *always* takes an argument, then declare it as such,
    > without the square brackets:


    Thanks for the quick, informative answer and sorry for the stupidity.
    ben
    --
    Posted via http://www.ruby-forum.com/.
    Ben Woodcroft, May 20, 2009
    #3
    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:
    876
    Grant Edwards
    Apr 6, 2005
  2. Alex Gittens
    Replies:
    1
    Views:
    442
    Peter Hansen
    Jul 10, 2005
  3. Steven Bethard
    Replies:
    0
    Views:
    384
    Steven Bethard
    May 10, 2006
  4. Ritesh Raj Sarraf

    optparse multiple arguments

    Ritesh Raj Sarraf, Jun 30, 2006, in forum: Python
    Replies:
    8
    Views:
    717
    Steven Bethard
    Jul 1, 2006
  5. Rocky Zhou
    Replies:
    3
    Views:
    298
    Steven Bethard
    Mar 18, 2007
Loading...

Share This Page