# Optparse: parse v. parse!?

**URL:** <https://www.ruby-forum.com/t/optparse-parse-v-parse/129617>\
**Category:** Ruby\
**Created:** [February 20, 2008, 3:57am UTC](https://www.ruby-forum.com/t/optparse-parse-v-parse/129617 "2008-02-20T03:57:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [February 20, 2008, 3:57am UTC](https://www.ruby-forum.com/t/optparse-parse-v-parse/129617/1 "2008-02-20T03:57:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [February 20, 2008, 5:46am UTC](https://www.ruby-forum.com/t/optparse-parse-v-parse/129617/2 "2008-02-20T05:46:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [February 20, 2008, 6:12am UTC](https://www.ruby-forum.com/t/optparse-parse-v-parse/129617/3 "2008-02-20T06:12:01Z")

</div>

Nobuyoshi N. 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?

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [February 20, 2008, 6:20am UTC](https://www.ruby-forum.com/t/optparse-parse-v-parse/129617/4 "2008-02-20T06:20:49Z")

</div>

7stud – wrote:

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