# Getopt/Declare v1.20

**URL:** <https://www.ruby-forum.com/t/getopt-declare-v1-20/82081>\
**Category:** Ruby\
**Created:** [January 17, 2007, 10:10am UTC](https://www.ruby-forum.com/t/getopt-declare-v1-20/82081 "2007-01-17T10:10:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [January 17, 2007, 10:10am UTC](https://www.ruby-forum.com/t/getopt-declare-v1-20/82081/1 "2007-01-17T10:10:32Z")

</div>

Getopt::Declare - Declaratively Expressed Command-Line Arguments via  
Regular Expressions

Getopt::Declare is _yet another_ command-line argument parser, one  
which is specifically designed to be powerful but exceptionally easy to  
use.

= SYNOPSIS

require “Getopt/Declare”  
args = Getopt::Declare.new(\<\<‘EOF’)

-q, --quiet quiet  
-f, --files files:if… input files  
-n, --number num:n a float number  
-i, --integer num:i an integer number

EOF

p args[‘-q’]  
p args[‘-f’]  
p args.unused

= What’s NEW in v1.20?

- Parameter definitions can now also be specified using GNU style help  
lines, like in the example above.
- Parameter definitions can now be recognized by the use either of one  
or more tabs (like before) or by 3 consecutive spaces. This simplifies  
parameter definitions quite a lot, particularly for those using editors  
that are not friendly to tabs.
- A couple of minor bug fixes here and there.
- Docs are now available at rubyforge.

= How to get it?

gem install getopt-declare

or download as a zip file from rubyforge.

= For more information

[http://getoptdeclare.rubyforge.org](http://getoptdeclare.rubyforge.org) - documentation  
[http://rubyforge.org/projects/getoptdeclare/](http://rubyforge.org/projects/getoptdeclare/) - project

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [January 17, 2007, 10:31am UTC](https://www.ruby-forum.com/t/getopt-declare-v1-20/82081/2 "2007-01-17T10:31:35Z")

</div>

The library looks groovy.

But I never knew that block quoted strings worked like this:

> args = Getopt::Declare.new(\<\<‘EOF’)  
> -q, --quiet quiet  
> -f, --files files:if… input files  
> -n, --number num:n a float number  
> -i, --integer num:i an integer number  
> EOF

That’s just completely cool! And it also makes a lot of sense: I think  
I’ll actually remember it now.

You can do somethin like this:

irb(main):008:0\> puts \<\<‘STRING\_END’.gsub(‘a’) {‘XXX’}  
irb(main):009:0’ bab  
irb(main):010:0’ aba  
irb(main):011:0’ STRING\_END  
bXXXb  
XXXbXXX

You can even do this (discovered just now in one of those, “surely this  
wont work too, will it?” Ruby moments)!..

irb(main):015:0\> puts \<\<BLOCK\_ONE\_END + \<\<BLOCK\_TWO\_END  
irb(main):016:0" hello  
irb(main):017:0" there  
irb(main):018:0" BLOCK\_ONE\_END  
irb(main):019:0" and how about  
irb(main):020:0" some more?  
irb(main):021:0" BLOCK\_TWO\_END  
hello  
there  
and how about  
some more?

Brilliant!

Anyone know when you do and don’t need to place ''s around the string  
terminator?

Cheers,  
Benjohn

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [January 17, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/getopt-declare-v1-20/82081/3 "2007-01-17T12:46:07Z")

</div>

> Anyone know when you do and don’t need to place ''s around the string  
> terminator?

You don’t, really, but it is better to use ‘’, to have other  
programmers not think you are actually appending a previously defined  
constant (and prevent your program mis-behaving because of that).

You also do need “” if you expect #{} to work, thou.

Also, you may want to get into the habit of using:

\<\<-‘EOF’  
EOF

The ‘-’ makes sure that Ruby will still read the block properly even if  
someone else touches the code and makes the EOF not show up in the  
first column.

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [January 17, 2007, 2:43pm UTC](https://www.ruby-forum.com/t/getopt-declare-v1-20/82081/4 "2007-01-17T14:43:33Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Anyone know when you do and don’t need to place ''s around the string  
> terminator?  
> ‘’ tells Ruby not to interpolate, while the default (without quotes) is  
> to do so. You can also use quotes (single or double) to use a more funky  
> delimiter. - lets you indent the ending delimiter.

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [January 17, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/getopt-declare-v1-20/82081/5 "2007-01-17T17:55:31Z")

</div>

Devin M. wrote:

> removed\_email\_address@domain.invalid wrote:
> 
> > Anyone know when you do and don’t need to place ''s around the string  
> > terminator?  
> > ‘’ tells Ruby not to interpolate, while the default (without quotes) is  
> > to do so. You can also use quotes (single or double) to use a more funky  
> > delimiter. - lets you indent the ending delimiter.

The above is correct, unlike (I think) some other answers in this  
thread. To be clear about it:  
a = \<\<FOO  
a#{1+1}  
FOO

b = \<\<‘FOO’  
b#{1+1}  
FOO

c = \<\<“FOO”  
c#{1+1}  
FOO

d = \<\<-FOO  
d#{1+1}  
FOO

e = \<\<-‘FOO’  
e#{1+1}  
FOO

f = \<\<-“FOO”  
f#{1+1}  
FOO

puts a, b, c, d, e, f  
#=\> 2  
#=\> #{1+1}  
#=\> 2  
#=\> 2  
#=\> #{1+1}  
#=\> 2

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [January 17, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/getopt-declare-v1-20/82081/6 "2007-01-17T18:10:34Z")

</div>

Phrogz wrote:

> puts a, b, c, d, e, f  
> #=\> 2  
> #=\> #{1+1}  
> #=\> 2  
> #=\> 2  
> #=\> #{1+1}  
> #=\> 2

Er, I meant:  
#=\> a2  
#=\> b#{1+1}  
#=\> c2  
#=\> d2  
#=\> e#{1+1}  
#=\> f2

---

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [January 17, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/getopt-declare-v1-20/82081/7 "2007-01-17T18:35:45Z")

</div>

> #=\> a2  
> #=\> b#{1+1}  
> #=\> c2  
> #=\> d2  
> #=\> e#{1+1}  
> #=\> f2

That’s a nasty bug in ruby you’ve found there 😉
