Assuming that neither the properties (REG1, REG2, etc) nor the values assigned to them have meaningful embedded spaces and commas, this can be parsed with Getopt::Long.
The "@{n,m}" spec syntax tells Getopt::Long to read multiple values after an option even without repeating the option flag, so you can use this spec to get gather together all the values that belong to the -cmdline_option flag. However, once you retrieve all the values for -cmdline_options you will only have an array of values. Getopt::Long doesn't know how to build HoA's for a single option flag so you will have to do it yourself, like this:
use strict; use warnings; use Getopt::Long; # fake populate ARGV my $sLine= <<EOF; -cmdline_option REG1=value1.1, value1.2, value1.3, value1.4 -cmdline_option REG2=value2.1, value2.2, value2.3, value2.4 -cmdline_option REG3=value3.1, value3.2, value3.3, value3.4 EOF @ARGV = split(/\s+/, $sLine); sub makeKeyValuePairsIntoHoA; # ==================================================== # parse the command line # ==================================================== my @aCmdLineOpts; GetOptions('-cmdline_option=s@{0,}' => \@aCmdLineOpts); my $hCmdline = makeKeyValuePairsIntoHoA(\@aCmdLineOpts); # see results foreach my $k (sort keys %$hCmdline) { my $v = $hCmdline->{$k}; print "$k:<@$v>\n"; } # ==================================================== # subroutine for making key-value pairs into an HoA # ==================================================== sub makeKeyValuePairsIntoHoA { my ($aValues) = @_; # consolidate arguments so that all the # values belonging to REG1= are in a single # comma delimited list without spaces. my $sAllInOne = join(' ', @$aValues); $sAllInOne =~ s/\s*,\s*/,/g; # split into key-value pairs on whitespace my @aKeyValuePairs = split(/\s+/, $sAllInOne); my %hKeyValuePairs = map { my ($k,$v) = /(\w+)+=(.*)/; $k => [ split(/,/, $v) ] } @aKeyValuePairs; return \%hKeyValuePairs; }
Best, beth
In reply to Re: Processing Complicated Command Line Options using Getopt
by ELISHEVA
in thread Processing Complicated Command Line Options using Getopt
by ganeshts
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |