[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[ANN] Getopt/Declare v1.20

gga

1/17/2007 9:06:00 AM

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

6 Answers

benjohn

1/17/2007 9:31:00 AM

0

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



gga

1/17/2007 11:44:00 AM

0


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

Devin Mullins

1/17/2007 1:43:00 PM

0

benjohn@fysh.org 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.

Gavin Kistner

1/17/2007 4:53:00 PM

0

Devin Mullins wrote:
> benjohn@fysh.org 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

Gavin Kistner

1/17/2007 5:08:00 PM

0

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

benjohn

1/17/2007 5:35:00 PM

0

> 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

That's a nasty bug in ruby you've found there ;-)