[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

optparse: parse v. parse! ??

7stud --

2/20/2008 2:57:00 AM

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-....

3 Answers

Nobuyoshi Nakada

2/20/2008 4:46:00 AM

0

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

7stud --

2/20/2008 5:12:00 AM

0

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-....

7stud --

2/20/2008 5:21:00 AM

0

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-....