kvh009 has asked for the wisdom of the Perl Monks concerning the following question:
There is a option to given a option file on the command line (i.e. --conf=file).
I need a way to parse the command line options looking for --conf=file, if so then do file and then parse the rest of the command line options (minus the --conf=file). In summary, I need to handle the --conf=file before looking at the other command line options to keep the priority correct.
Obviously, this doesn't do what I want as the 'do' will be executed after parsing the command line options.my %help_options = ( "color!" => sub { $options{USE_COLOR} = $_[1] }, "conf=s" => sub { $options{CONF} = $_[1] }, ); GetOptions( %help_options ); do $options{CONF} if defined $options{CONF};
script.pl --conf=file --color should always set $options{USE_COLOR} = 1 regardless of what's in file.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Prioritizing command line options
by grinder (Bishop) on Jan 19, 2002 at 02:55 UTC | |
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:
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
| [reply] [Watch: Dir/Any] [d/l] |
(RhetTbull) Re: Prioritizing command line options
by RhetTbull (Curate) on Jan 19, 2002 at 02:52 UTC | |
These two subs are defined like this (there are probably cleaner ways to process the args but this works fine for my simple prog.) For various reasons I wrote a wrapper around Config::IniFiles. One reason was to allow multivalued parameters (which Config::Ini did but had other issues). Here's that custom wrapper:
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Prioritizing command line options
by VSarkiss (Monsignor) on Jan 19, 2002 at 02:54 UTC | |
You don't show what's in file, but why not just use another option variable? (Note, this is untested.) That's not necessarily the easiest or best way to accomplish it, but it's probably the smallest change to your example code that would do it. On a separate note, I cringe at the idea that your program variables are spread among its own and the external file. (In other words, the external file knows the name of the %options hash and the particular key values, so if you change one you have to change the other.) I'd much rather use a "ini" style file with var = value pairs or some such, where I can map the keys to my variables through a hash or something. Of course, I don't know what limitations you're working under, so maybe this isn't practical for you, but if it is, I'd really recommend the change. | [reply] [Watch: Dir/Any] [d/l] [select] |