Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Prioritizing command line options

by grinder (Bishop)
on Jan 19, 2002 at 02:55 UTC ( [id://139983]=note: print w/replies, xml ) Need Help??


in reply to Prioritizing command line options

I know that Getopt::Mixed will let you do this, as it uses a serial approach to parsing the command line. On the other hand, a module such as Getopt::Long will not, as you can't intervene (Although I expect to be corrected if am I wrong on this point) in GetOptions, since there is no callback interface.

Getopt::Mixed turns the problem on its head, by the fact that you specify a template string for what arguments are to be recognised, and then you repeatedly call a function until it returns no more arguments. Consider:

Getopt::Mixed::init( 'c=s f=s conf>c foo>f'); while( my( $option, $value, $pretty ) = Getopt::Mixed::nextOption() ) +{ OPTION: { $option eq 'c' and do { # you want to do more error checking that this... eval { do $value }; die "Cannot load config file $value: $@\n"; last OPTION; }; $option eq 'f' and do { $Foo = $value; last OPTION; }; # ... } } Getopt::Mixed::cleanup();

In this way, the config file specified by --conf will be done at the point at which it is encountered on the command line.

The only (minor) drawback is that this module is not bundled with the core distribution, you have to download it from CPAN. But it is a pure-perl module and so installs with a minimum of fuss.

--
g r i n d e r
print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-03-28 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found