Getopt/Declare v1.20


#1

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 - documentation
http://rubyforge.org/projects/getoptdeclare/ - project


#2

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


#3

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.


#4

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.


#5

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


#6

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


#7

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

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