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. |