[ANN] getopt-1.3.0

Hi all,

I'm pleased to announce the release of getopt-1.3.0. This release includes a Getopt::Long class.

== What is it?
A sane replacement for command line parsing libraries like getoptlong and optparse.

== How does it work
It's similar to the old Getoptlong class, but it's less verbose, more flexible and (*shock*) it returns a hash.

== Synopsis
require "getopt/long"
include Getopt

opts = Long.getopts(
    [ "--name", "-n", REQUIRED ],
    [ "--age", "-a", OPTIONAL ],
    [ "--more", "-m", INCREMENT],
    [ "--verbose", "-v", BOOLEAN]
)

# User passes "--name dan --age=35 -m -m -v"
opts == {
    "n" => "dan",
    "name" => "dan",
    "a" => "35",
    "age" => "35",
    "m" => 2,
    "more" => 2,
    "v" => true,
    "verbose" => true
}

The dups are there to give you maximum flexibility with regards to how you access your option, e.g. you could do:

if opts["v"] # or
if opts["verbose"]

This, along with the removal of "--" and "-", saves on the carpal.

You'll also notice that you can use the "=" syntax for long options, e.g. "--age=35", if you happen to prefer that style.

See the README file for more details.

== Where can I get it?
You should be able to do "gem install getopt" very soon. Or, you can get the tarball via the RAA.

== Why I wrote this
My main gripe with the getoptlong library currently in the standard library is that it doesn't return a hash, yet gives you partial hash behavior. This was both confusing and annoying, since the first thing I (and everyone else)
does is collect the results into a hash for later processing. Just yesterday Ara Howard did this in ruby-talk:163940. It's also too verbose.

My main gripe with the optparse library (also in the standard library) is
that it treats command line processing like event processing. It's too
complex, when 90% of the time all you want to do is slurp the command line options into a hash.

So, I did something utterly novel with this library. I collected the command line options ... (wait for it) ... into a hash! Then I give that hash to you, aliases and all. I did get some ideas from Perl's Getopt::Long library, but this is in no way a port of that module (which supports POSIX parsing, GNU parsing, more option types, etc). My goal was to provide the functionality that I felt would cover the vast majority of common cases, yet still provide a little extra spice with switch types (REQUIRED, OPTIONAL, etc).

== Future Plans
There are a few extra things I plan to add but I do not plan on this library ever becoming as feature rich as, say, Perl's Getopt::Long module. I'm more interested in just making it usable and easy.

Enjoy!

Dan

Hi all,

I'm pleased to announce the release of getopt-1.3.0. This release includes a

        [...]

== How does it work
It's similar to the old Getoptlong class, but it's less verbose, more flexible
and (*shock*) it returns a hash.

== Synopsis
require "getopt/long"
include Getopt

opts = Long.getopts(
   [ "--name", "-n", REQUIRED ],
   [ "--age", "-a", OPTIONAL ],
   [ "--more", "-m", INCREMENT],
   [ "--verbose", "-v", BOOLEAN]
)

# User passes "--name dan --age=35 -m -m -v"

Will one be able to write that as
   -mmv --name dan --age=35
or not? I expect it makes parsing trickier. I don't do it for
single letter options that take parameters except
tar -cvf tarfile -
so I could live without it.

Thank you. Simplifying the interface is usually the right thing to
do :slight_smile:

        Thank you,
        Hugh

···

On Sat, 5 Nov 2005, Daniel Berger wrote:

Daniel Berger wrote:

Hi all,

I'm pleased to announce the release of getopt-1.3.0. This release includes a
Getopt::Long class.

Now we need to get this into the standard libraries. :slight_smile:

Steve

Hugh Sasse wrote:

<snip>

# User passes "--name dan --age=35 -m -m -v"

Will one be able to write that as
   -mmv --name dan --age=35
or not? I expect it makes parsing trickier. I don't do it for
single letter options that take parameters except tar -cvf tarfile -
so I could live without it.

Not at the moment, but it wouldn't be difficult to add it.

Thank you. Simplifying the interface is usually the right thing to
do :slight_smile:

        Thank you,
        Hugh

You're welcome. :slight_smile:

- Dan

I believe CommandLine::OptionParser can do this
(http://rubyforge.org/docman/view.php/632/170/index.html\).
Recommended.
-lv

Hugh Sasse wrote:

[snip]

···

Will one be able to write that as
   -mmv --name dan --age=35
or not? I expect it makes parsing trickier. I don't do it for
single letter options that take parameters except tar -cvf tarfile -
so I could live without it.

Thank you. Simplifying the interface is usually the right thing to
do :slight_smile:

        Thank you,
        Hugh

Daniel Berger wrote:

Hugh Sasse wrote:

<snip>

>># User passes "--name dan --age=35 -m -m -v"
>
>
> Will one be able to write that as
> -mmv --name dan --age=35
> or not? I expect it makes parsing trickier. I don't do it for
> single letter options that take parameters except
> tar -cvf tarfile -
> so I could live without it.

Not at the moment, but it wouldn't be difficult to add it.

Good way to jinx myself, eh?

Anyway, you can do this as of 1.3.1, on the RAA now (and the gem should
be available shortly).

Regards,

Dan