Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: RFC Getopt::Hash

by jdporter (Canon)
on Aug 09, 2005 at 15:11 UTC ( #482238=note: print w/replies, xml ) Need Help??


in reply to RFC Getopt::Hash

You seem to have overlooked the fact that Getopt::Long already supports getting options into a hash. (See the 'Storing options in a hash' section of the Getopt::Long man page.)
use Getopt::Long; my %opt = ( 'option_name' => 'default_value', 'element' => 0, 'count' => 5, ); GetOptions( \%opt, 'option_name|on:s', 'element|e!', 'count|cnt|c=i', );

Replies are listed 'Best First'.
Re^2: RFC Getopt::Hash
by radiantmatrix (Parson) on Aug 09, 2005 at 16:23 UTC

    Yes, I missed that. However, that's only half of my purpose, as the other half is to get the options into a hash and set default values in one statement.

    I shall make a note to update my POD to clarify that.

    <-radiant.matrix->
    Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
      Next can you make GO::L handle short and long options sanely?

        I believe it is already done: Getopt::Mixed. The syntax is slightly different from Getopt::Long, so it isn't a drop-in replacement. Also, by default, it exports a bunch of scalars into the calling package (bad, IMO) -- the non-default behavior is pretty nice, though:

        use Getopt::Mixed 'nextOption'; Getopt::Mixed::init('option1=s>o', 'option2:i>o2'); my %opt = ( option1 => 'default1', option2 => 'default2', ); while ( my ($k, $v) = nextOption() ) { $opt{$k} = $v } Getopt::Mixed::cleanup(); ## what's left is now in @ARGV

        Using nextOption lets you process serially, too, which can be nice sometimes.

        <-radiant.matrix->
        Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
        The Code that can be seen is not the true Code
        "In any sufficiently large group of people, most are idiots" - Kaa's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://482238]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2018-06-21 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (117 votes). Check out past polls.

    Notices?