I don't think you're specifying the problem well enough. The line you give as an example of an argument list:
getStuff configs --force=all --out out.txt -v -a
will *not* necessarily be parsed into a list like the one you posted. It is *entirely* up to the program who is receiving these arguments how to parse them, and what to do with them.

As an example, you are implying that the "--out" option should be followed by a string argument (this is implied by the fact that in your list, you have the line "--out out.txt". But how can you possibly know that, given an arbitrary set of options? "--out" could simply be a boolean flag, not taking any additional arguments, and "out.txt" could be a file supplied as an argument to the program that you're running.

The point is, your post suggest no way of discerning this information, and w/o that, there's really no way we can help you parse the config options. And in fact, although I don't know what your script is trying to do, I would strongly discourage you from trying to parse the command line options to another program--unless you *are* that other program, you have no way of knowing what the options mean, and what arguments they take, etc.

Now, if you *do* know that information, then you can use Getopt::Long to parse the arguments, kind of like this:

use Getopt::Long; use Data::Dumper; { local @ARGV = ("configs", "--force=all", "--out", "out.txt", " +-v", "-a"); GetOptions(\my %opts, "force=s", "out=s", "v", "a"); print Dumper \%opts; print Dumper \@ARGV; }
This gives:
$VAR1 = { 'out' => 'out.txt', 'a' => 1, 'v' => 1, 'force' => 'all' }; $VAR1 = [ 'configs' ];
But just as a demonstration of what I was saying above, if we change that GetOptions spec to this:
GetOptions(\my %opts, "force=s", "out", "v", "a");
we now get this:
$VAR1 = { 'out' => 1, 'a' => 1, 'v' => 1, 'force' => 'all' }; $VAR1 = [ 'configs', 'out.txt' ];
Which just goes to show you that unless you know what, for example, "--out" means, you're really out of luck.

In reply to Re: join this by btrott
in thread splitting command-line arguments by IraTarball

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.