optparse: parse v. parse! ??

Discussion in 'Ruby' started by 7stud --, Feb 20, 2008.

  1. 7stud --

    7stud -- Guest

    Here's a simple example that looks for two options--one with a required
    value and one with an optional value:

    require 'optparse'

    opts = OptionParser.new do |opts|
    puts 'hello'

    #option where value is required:
    opts.on("-t=RANDOM_CHARS") do |val|
    puts 'in first on()'

    puts "-t option entered with value=#{val}"
    end

    #option where value is optional:
    opts.on("-y [=RANDOM_CHARS]") do |val|
    puts "-y option entered with value=#{val}"
    end

    end

    opts.parse!
    p ARGV

    --output:--
    $ruby optparseEx2.rb -t hi -y

    hello
    in first on()
    -t option entered with value=hi
    -y option entered with value=
    []


    However, when I change the line:

    opts.parse!

    to:

    opts.parse

    I get this output:

    $ruby optparseEx2.rb -t hi -y

    hello
    ["-t", "hi", "-y"]

    which indicates that none of the on() handlers executed. My reading of
    parse() is that it's the same as parse!() except that parse() doesn't
    remove the option/values from ARGV.
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Feb 20, 2008
    #1
    1. Advertising

  2. Hi,

    At Wed, 20 Feb 2008 11:57:09 +0900,
    7stud -- wrote in [ruby-talk:291731]:
    > which indicates that none of the on() handlers executed. My reading of
    > parse() is that it's the same as parse!() except that parse() doesn't
    > remove the option/values from ARGV.


    No, parse doesn't have default arguments. This hasn't changed
    since it was imported.

    --
    Nobu Nakada
    Nobuyoshi Nakada, Feb 20, 2008
    #2
    1. Advertising

  3. 7stud --

    7stud -- Guest

    Nobuyoshi Nakada wrote:
    > Hi,
    >
    > At Wed, 20 Feb 2008 11:57:09 +0900,
    > 7stud -- wrote in [ruby-talk:291731]:
    >> which indicates that none of the on() handlers executed. My reading of
    >> parse() is that it's the same as parse!() except that parse() doesn't
    >> remove the option/values from ARGV.

    >
    > No, parse doesn't have default arguments.


    Huh?
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Feb 20, 2008
    #3
  4. 7stud --

    7stud -- Guest

    7stud -- wrote:
    > Nobuyoshi Nakada wrote:
    >> Hi,
    >>
    >> At Wed, 20 Feb 2008 11:57:09 +0900,
    >> 7stud -- wrote in [ruby-talk:291731]:
    >>> which indicates that none of the on() handlers executed. My reading of
    >>> parse() is that it's the same as parse!() except that parse() doesn't
    >>> remove the option/values from ARGV.

    >>
    >> No, parse doesn't have default arguments.

    >
    > Huh?


    Well, after scanning through the docs again, I found this in one of the
    examples:

    options = OptparseExample.parse(ARGV)

    ...and now your cryptic post makes sense.


    Usage
    ------

    opts.parse! #removes options/values from ARGV

    opts.parse(ARGV) #doesn't change ARGV
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Feb 20, 2008
    #4
    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. David Goodger

    Re: Iterating over optparse options

    David Goodger, Jun 25, 2003, in forum: Python
    Replies:
    0
    Views:
    991
    David Goodger
    Jun 25, 2003
  2. pascal barbedor

    blanks embedded in python 2.3 optparse

    pascal barbedor, Jul 29, 2003, in forum: Python
    Replies:
    3
    Views:
    327
    =?ISO-8859-2?Q?Nagy_L=E1szl=F3_Zsolt?=
    Jul 29, 2003
  3. Gregory (Grisha) Trubetskoy

    Required options in optparse

    Gregory (Grisha) Trubetskoy, Oct 4, 2003, in forum: Python
    Replies:
    0
    Views:
    414
    Gregory (Grisha) Trubetskoy
    Oct 4, 2003
  4. Daniel Ortmann
    Replies:
    0
    Views:
    259
    Daniel Ortmann
    Oct 13, 2003
  5. John Goebel

    Optparse and varible nargs

    John Goebel, Nov 3, 2003, in forum: Python
    Replies:
    2
    Views:
    1,015
    Paddy McCarthy
    Nov 4, 2003
Loading...

Share This Page