Hi,
I'm trying to build a generic Parsing Arguments functions that would
be driven by an array of possible options that is defined by a
specific program. I'm trying to use this feature of GetOptions:

my %h = ();
GetOptions (\%h, 'length=i');       # will store in $h{length}

But what I would like to do is to not have to hard code the 'length=i'
option directly in the argument for GetOptions, but rather this be
based on some list. Here is what I would like to do (but the options
part is not working). Anyone have any suggestions or recommendations
(maybe I'm going at this all wrong)?


use Getopt::Long;
use Data::Dumper qw(Dumper);
our %MyArgs = ();
our %OptList = ();
sub ParseArgs {
        GetOptions(%OptList) or Getopt::Long::HelpMessage(2);
}


@GlobalSwitches = ("test","showlog");       # These will be available
to any program
@LocalSwitches = ("noadd", "noemail");     # These will be placed in
an ini file
@OptionList = ("strategy","country");          # These will be placed
in an ini file
# strategy option could contain: "LT" or "MT" or "ST" or "ALL"
# country option could contain a ISO country code, I would like to
support multiple values

@SwitchList = (@GlobalSwitches, @LocalSwitches);

foreach my $switch (@SwitchList) {$OptList{$switch} = \
$MyArgs{$switch}}
print "Available command line args \n" . Dumper(%MyArgs) ;

foreach my $option (@OptionList) {$OptList{$option} = \
$MyArgs{$option}}
print "Available command line args is now \n" . Dumper(%MyArgs) ;

ParseArgs;

# Switch Results
print "loading into DB is turned OFF\n" if $MyArgs{noadd};
print "E-mailing results is turned OFF\n" if $MyArgs{noemail};
print "Running in TEST Mode\n" if $MyArgs{test};
print "Running in SHOWLOG Mode\n" if $MyArgs{showlog};

#Option Results
print "Strategy is: $MyArgs{strategy} \n" if $MyArgs{strategy};
print "Country is: $MyArgs{country} \n" if $MyArgs{country};

exit;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to