Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Command-line option parsing

Reply
Thread Tools

Command-line option parsing

 
 
Eric J. Roode
Guest
Posts: n/a
 
      12-27-2005
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
 
Reply With Quote
 
 
 
 
Lou Vanek
Guest
Posts: n/a
 
      12-28-2005
i like CommandLine::OptionParser
(http://rubyforge.org/docman/view.php/632/170/index.html)
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.


Eric J. Roode 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 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,
>




 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-28-2005
Eric J. Roode <(E-Mail Removed)> 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

 
Reply With Quote
 
Jim Freeze
Guest
Posts: n/a
 
      12-28-2005
------=_Part_55632_23363128.1135779815810
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On 12/27/05, Lou Vanek <(E-Mail Removed)> wrote:
>
> i like CommandLine::OptionParser
> (http://rubyforge.org/docman/view.php/632/170/index.html)
> 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 eon=
s
ago. The new docs are at:
http://rubyforge.org/docman/view.php...ocs.index.html

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 nee=
d
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

------=_Part_55632_23363128.1135779815810--


 
Reply With Quote
 
Steve Litt
Guest
Posts: n/a
 
      12-28-2005
On Wednesday 28 December 2005 08:37 am, Robert Klemme wrote:
> Eric J. Roode <(E-Mail Removed)> 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.troubleshooters.com
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-28-2005
Jim Freeze <(E-Mail Removed)> 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

 
Reply With Quote
 
Jim Freeze
Guest
Posts: n/a
 
      12-28-2005
------=_Part_57353_8112205.1135789737433
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On 12/28/05, Robert Klemme <(E-Mail Removed)> 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

------=_Part_57353_8112205.1135789737433--


 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      12-28-2005
mathew <(E-Mail Removed)> 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
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      12-28-2005
"Robert Klemme" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
Reply With Quote
 
Jim Freeze
Guest
Posts: n/a
 
      12-29-2005
------=_Part_61088_27178231.1135816236587
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 =3D> %w(--require -r),
pt_description =3D> "Require the LIBRARY "+
"before executing your script",
:arg_description =3D> "LIBRARY",
pt_found =3D> get_arg,
pt_not_found =3D> required

option :names =3D> %w(--inplace -i),
:arity =3D> [0,1],
pt_description =3D> "Edit ARGV files in place",
:arg_description =3D> "[EXTENSION]",
pt_found =3D> 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

------=_Part_61088_27178231.1135816236587--


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex for <option> ... </option> John Perl Misc 10 01-29-2009 11:04 PM
page.aspx?option - how to detect "option" Kevin Blount ASP .Net 6 11-28-2006 09:21 PM
DHCP relay agent versus Option 3; Routers Option lcorrigan Cisco 2 09-27-2006 05:18 PM
no 'option' in aspx file means 'option'="false"? Cas ASP .Net 5 08-28-2006 10:36 AM
g++ -pg option and -shared option Julien ROUZIERES C++ 1 12-21-2004 02:30 PM



Advertisments