Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > optparse: parse v. parse! ??

Reply
Thread Tools

optparse: parse v. parse! ??

 
 
7stud --
Guest
Posts: n/a
 
      02-20-2008
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/.

 
Reply With Quote
 
 
 
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      02-20-2008
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

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      02-20-2008
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/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      02-20-2008
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM
parse inside of html tags jjliu Perl 3 10-11-2003 11:34 AM
Parse Text File and Output to File John M. Lembo Perl 0 08-01-2003 04:34 PM
Parse an xml file with line breaks in the beginning Raj Mudaliar Perl 0 07-14-2003 06:00 PM
[TABLE NOT SHOWN] problem with HTML::Parse Mitchua Perl 3 07-13-2003 11:38 PM



Advertisments