[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Command-line option parsing

Eric J. Roode

12/27/2005 11:38:00 PM

Greetings,

I'm new to ruby (from Perl). I notice there are several command-line
options parsing modules. The two that stuck out were GetoptLong and
OptionsParser. GetoptLong suffers from being "the same old Getopt".
OptionsParser claims to be "more advanced and more ruby-like", but has
extremely poor documentation.

Are these the best two modules out there? Is there an emerging
"standard" module? (In Perl, Getopt::Long is by far the most commonly-used
module).

(I apologize if this is a FAQ; I read the FAQ and did a google scan of past
articles. Most of what I found was authors announcing their own options-
parsing modules).

Thanks in advance,

--
Eric Roode
17 Answers

vanekl

12/28/2005 12:09:00 AM

0

mathew

12/28/2005 4:15:00 AM

0

Eric J. Roode wrote:
> GetoptLong suffers from being "the same old Getopt".

Well, what do you expect from an options parser? It's pretty much a
solved problem, and using the standard getopt algorithms has the
advantage that users know what to expect.


mathew
--
<URL:http://www.pobox.com/...
My parents went to the lost kingdom of Hyrule
and all I got was this lousy triforce.

Robert Klemme

12/28/2005 1:38:00 PM

0

Eric J. Roode <sdn.girths00869@zoemail.net> wrote:
> Greetings,
>
> I'm new to ruby (from Perl). I notice there are several
> command-line options parsing modules. The two that stuck out were
> GetoptLong and OptionsParser. GetoptLong suffers from being "the
> same old Getopt". OptionsParser claims to be "more advanced and more
> ruby-like", but has extremely poor documentation.
>
> Are these the best two modules out there? Is there an emerging
> "standard" module? (In Perl, Getopt::Long is by far the most
> commonly-used module).

I used to use GetopLong in the beginning but switched to OptionParser.
Agreee, the documentation could be better but if you look carefully at the
example given in RDoc you get pretty much everything you need. I'm not sure
about a standard parser but my impression was that OptionParser is more
widely used. But I may be wrong here.

Kind regards

robert

Jim Freeze

12/28/2005 2:24:00 PM

0

On 12/27/05, Lou Vanek <vanek@acd.net> wrote:
>
> i like CommandLine::OptionParser
> (http://rubyforge.org/docman/view.php/632/170/...)
> because it's documentation is better than average as well
> as its features.
> it doesn't appear to be the ruby standard, however, but for
> in-house projects there's a lot to like.


Yikes.
That is old documenetation and the link on Rubyforge was to be replaced eons
ago. The new docs are at:
http://rubyforge.org/docman/view.php/632/233/posted-docs....

There you will find the option parser docs after Application section. For
the most part, if you use the CommandLine::Application class, you don't need
to get
your hands that dirty with CommandLine::OptionParser.


If you have any questions, just post a question or send me
a private email. I plan to post a video on the usage this week. Maybe a
rails like video will generate more interest. :)

--
Jim Freeze

Steve Litt

12/28/2005 3:20:00 PM

0

On Wednesday 28 December 2005 08:37 am, Robert Klemme wrote:
> Eric J. Roode <sdn.girths00869@zoemail.net> wrote:
> > Greetings,
> >
> > I'm new to ruby (from Perl). I notice there are several
> > command-line options parsing modules. The two that stuck out were
> > GetoptLong and OptionsParser. GetoptLong suffers from being "the
> > same old Getopt". OptionsParser claims to be "more advanced and more
> > ruby-like", but has extremely poor documentation.
> >
> > Are these the best two modules out there? Is there an emerging
> > "standard" module? (In Perl, Getopt::Long is by far the most
> > commonly-used module).
>
> I used to use GetopLong in the beginning but switched to OptionParser.
> Agreee, the documentation could be better but if you look carefully at the
> example given in RDoc you get pretty much everything you need. I'm not
> sure about a standard parser but my impression was that OptionParser is
> more widely used. But I may be wrong here.
>
> Kind regards
>
> robert

Does OptionParser come with standard Ruby?

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


Robert Klemme

12/28/2005 4:48:00 PM

0

Jim Freeze <jim@freeze.org> wrote:

> If you have any questions, just post a question or send me
> a private email. I plan to post a video on the usage this week. Maybe
> a rails like video will generate more interest. :)

Will there be stunt scenes with options and a high speed car chase?

robert

Jim Freeze

12/28/2005 5:09:00 PM

0

On 12/28/05, Robert Klemme <bob.news@gmx.net> wrote:
>
>
> Will there be stunt scenes with options and a high speed car chase?


It will be action packed. There will be trains, rubies
and high speed typing! ;)


--
Jim Freeze

Eric J. Roode

12/28/2005 10:51:00 PM

0

mathew <meta@pobox.com> wrote in news:wjosf.9901$9e.8623
@tornado.texas.rr.com:

> Eric J. Roode wrote:
>> GetoptLong suffers from being "the same old Getopt".
>
> Well, what do you expect from an options parser? It's pretty much a
> solved problem, and using the standard getopt algorithms has the
> advantage that users know what to expect.

Well... that's not *bad*, but I guess I had expected something a bit more
ruby-like. Whatever that means. :-)


--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`

Eric J. Roode

12/28/2005 11:01:00 PM

0

"Robert Klemme" <bob.news@gmx.net> wrote in
news:41fiouF1e6diiU1@individual.net:

> I used to use GetopLong in the beginning but switched to OptionParser.
> Agreee, the documentation could be better but if you look carefully at
> the example given in RDoc you get pretty much everything you need.
> I'm not sure about a standard parser but my impression was that
> OptionParser is more widely used. But I may be wrong here.

I disagree about the example being all you need.

Here's the first option in the example:

# Mandatory argument.
opts.on("-r", "--require LIBRARY",
"Require the LIBRARY before executing your script") do |lib|
options.library << lib
end

I am new to ruby, so I don't know what "options.library << lib" means. I
thought that that introduced a here-doc string.

The second example:

# Optional argument; multi-line description.
opts.on("-i", "--inplace [EXTENSION]",
"Edit ARGV files in place",
" (make backup if EXTENSION supplied)") do |ext|
...

It took me a lot of staring at the code before it dawned on me that it
was the *brackets* that made it an optional argument.

The final example:

# Another typical switch to print the version.
opts.on_tail("--version", "Show version") do
puts OptionParser::Version.join('.')
exit
end

I can't figure out how the on_tail method differs from the on method or
the on_head method. Reading the source isn't helping. The example
sucks.

The previous example has this comment:

# No argument, shows at tail. This will print an options summary.

"Shows at tail"??

At the end of the parse() method (and why is it self.parse, not just
parse?), there is this:

opts.parse!(args)

I can't tell what function that performs for the class.

I can't tell how to make an option mandatory, how to configure it so that
some options require certain others or conflict with certain others.

It looks like a fine, powerful module. But its lack of documentation
makes it pretty much useless for anyone who doesn't already know how to
use it.

I come from the Perl world. You simply do not release a module to Perl's
CPAN unless it is thoroughly documented. The vast majority of CPAN
modules have quite good documentation -- or at least, thorough
documentation. I'm quite surprised to find that the situation is so
different in the ruby world.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`

Jim Freeze

12/29/2005 12:31:00 AM

0

Hi Eric

You may want to give CommandLine a try.
From the examples you give, this is how I would write an app:

require 'rubygems'
require 'commandline'

class MyApp < CommandLine::Application
def initialize
# Mandatory argument
option :names => %w(--require -r),
:opt_description => "Require the LIBRARY "+
"before executing your script",
:arg_description => "LIBRARY",
:opt_found => get_arg,
:opt_not_found => required

option :names => %w(--inplace -i),
:arity => [0,1],
:opt_description => "Edit ARGV files in place",
:arg_description => "[EXTENSION]",
:opt_found => get_arg,
end

def main
#put your code here
p opts
end
end#class MyApp

I don't know what #on_tail does either.


--
Jim Freeze