# Something more compact for getopt

**URL:** <https://www.ruby-forum.com/t/something-more-compact-for-getopt/74728>\
**Category:** Ruby\
**Created:** [November 7, 2006, 10:54am UTC](https://www.ruby-forum.com/t/something-more-compact-for-getopt/74728 "2006-11-07T10:54:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [November 7, 2006, 10:54am UTC](https://www.ruby-forum.com/t/something-more-compact-for-getopt/74728/1 "2006-11-07T10:54:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [November 7, 2006, 10:54am UTC](https://www.ruby-forum.com/t/something-more-compact-for-getopt/74728/2 "2006-11-07T10:54:28Z")

</div>

On 11/2/06, Maciej P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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/index.html](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html)  
[2]  
[http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/projects/mongrel\_service/bin/mongrel\_service?revision=358&root=mongrel](http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/projects/mongrel_service/bin/mongrel_service?revision=358&root=mongrel)

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [November 7, 2006, 10:54am UTC](https://www.ruby-forum.com/t/something-more-compact-for-getopt/74728/3 "2006-11-07T10:54:46Z")

</div>

Maciej P. wrote:

> 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.rubyforge.org](http://facets.rubyforge.org)).

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/project/usage/](http://raa.ruby-lang.org/project/usage/))

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.
