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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.