[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Something more compact for getopt

Maciej Pilichowski

11/2/2006 7:18:00 AM

Hello,

I use GetoptLong but I miss something much shorter in usage like
GetoptLong in Perl. In Perl I could write

GetOptions('re_from=s' => \$re_from, 're_to=s' => \$re_to, 'do' =>
\$do_do, 'enum' => \$enum, 'from=i' => \$from, 'step=i' => \$step,
'width=i' => \$width);

So in one line -- or two -- I set the possible arguments, their
types, assignment to variables and all the if-checking is done for me.
No need to write loop to check all parameters like in Ruby.
Is any package available which do similar thing for Ruby?

Thanks in advance, have a good day
bye

--
Maciej "MACiAS" Pilichowski http://bantu.fm.i...
2 Answers

Jano Svitok

11/2/2006 10:32:00 AM

0

On 11/2/06, Maciej Pilichowski <bantu@skasujtopoczta.fm> wrote:
> Hello,
>
> I use GetoptLong but I miss something much shorter in usage like
> GetoptLong in Perl. In Perl I could write
>
> GetOptions('re_from=s' => \$re_from, 're_to=s' => \$re_to, 'do' =>
> \$do_do, 'enum' => \$enum, 'from=i' => \$from, 'step=i' => \$step,
> 'width=i' => \$width);
>
> So in one line -- or two -- I set the possible arguments, their
> types, assignment to variables and all the if-checking is done for me.
> No need to write loop to check all parameters like in Ruby.
> Is any package available which do similar thing for Ruby?
>
> Thanks in advance, have a good day
> bye

try optparse [1], though not that concise, it's pretty nice and easy
to use. You can find pretty smart usage in [2]. look for # default
options

[1] http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/...
[2] http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/projects/mongrel_service/bin/mongrel_service?revision=358&ro...

Trans

11/2/2006 1:52:00 PM

0


Maciej Pilichowski wrote:
> Hello,
>
> I use GetoptLong but I miss something much shorter in usage like
> GetoptLong in Perl. In Perl I could write
>
> GetOptions('re_from=s' => \$re_from, 're_to=s' => \$re_to, 'do' =>
> \$do_do, 'enum' => \$enum, 'from=i' => \$from, 'step=i' => \$step,
> 'width=i' => \$width);
>
> So in one line -- or two -- I set the possible arguments, their
> types, assignment to variables and all the if-checking is done for me.
> No need to write loop to check all parameters like in Ruby.
> Is any package available which do similar thing for Ruby?
>
> Thanks in advance, have a good day
> bye

I'll give you two options. One that I worte that isn't as concise as
you've asked, but it has it's own form of simplicity (see
http://facets.rub...).

require 'facet/command.rb'

class MyCommand < Cosole::Command
def __re_from(s); $re_from = s.to_s; end
def __re_to(s); $re_to = s.to_s; end
def __do; $do_do = true; end
def __enum; $enum = true; end
def __from(i); $from = i.to_i; end
def __step(i); $step = i.to_i; end
def __width(i); $width = i.to_i; end
# ...
end

It can't handle using the '=' sign yet, but I plan to add taht soon
with a few other featuers.

The other is the usage lib (see
http://raa.ruby-lang.org/proj...)

usage = Usage.new "-t to_name -f from_name files_to_send..."

puts usage.to_name # => the value after the -t
puts usage.from_name # => the value after -f
puts usage.files_to_send # => an array of filenames

HTH,
T.