Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: GetOpt::Long usage style

by demerphq (Chancellor)
on Mar 08, 2003 at 19:58 UTC ( [id://241429]=note: print w/replies, xml ) Need Help??


in reply to Re: GetOpt::Long usage style
in thread GetOpt::Long usage style

Yeah I do use this style as well. However it still presents duplication when you want to provide defaults or multiple names or types for options (which rules out a quick key %opts), in fact its worse because you dont have compile time var name checking of the var names if they get out of synch. In other words the connection between destination and source becomes too loose and it becomes really easy to make a subtle error

my %opt={verbose=>0,all=>0); GetOptions(\%opt,'talkative|verbose','everything|all');

So what happens if we werent careful and do it like

GetOptions(\%opt,'talkative|verbose','all|everything');

Oops, now the --all options goes to the wrong place.

Anyway, I still would use the %hash style as well for some situations, and as the interface to Getopt::Long is so flexible you can mix the two:

GetOptions(\%opts,'Debug=i'=>\(my $Debug=0),'verbose','all');

And then theres also using subs for the options... Mmm fun!


---
demerphq


Replies are listed 'Best First'.
Re3: GetOpt::Long usage style
by dragonchild (Archbishop) on Apr 08, 2003 at 22:50 UTC
    Personally, I tend to keep thingsl ike synonyms in a data structure of their own. So, I might do something like:
    my %options = ( verbose => 0, everything => 1, ); my %synonyms = ( verbose => [ qw( talkative )], everything => [ qw( all )], ); GetOptions( \%options, ( map { join\('|', $_, @{ $synonyms{$_} || []}) } keys %options ), );
    This way you can, at a glance, see exactly what your synonyms are, laid out nice as can be. A similar thing can be done with the "=i", "=s", etc.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://241429]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-03-28 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found