[EMAIL PROTECTED] wrote:
I've been trying for an hour or so to use Getopt::Long, but I just don't
get it... I've been reading FAQ's & docs but I need some more examples to
know how it works.

Well, I want to process arguments like:

perl command.pl arg_needed [ --flag1 one=something two=otherthing | --flag2
| --flag3 ]

and I get:

$var=arg_needed
$one=something
$two=otherthing


How can I achieve this?

TIA,
m4c.

Try something like the following. You will have to decide what it means and if it is valid for the user to specify:
'--flag one=' or '--flag one'
The code below accepts the first, but dies on the second. It will process args up to the next flag and then return.

use strict;
use warnings;

use Getopt::Long;
use Pod::Usage;

use vars qw(
%flag_opts
);

sub opt_multi {
my $flag = shift;
my $arg = shift;

do {
my ( $k, $v ) = split( '=', $arg, 2 );
if ( defined($k) && defined($v) ) {
$flag_opts{$k} = $v;
} else {
die "Error: Invalid Arguments to $flag\n";
}
} while ( @ARGV
&& ($ARGV[0] !~ /^--?/)
&& ($arg = shift(@ARGV)) );
}

GetOptions(
'flag=s', \&opt_multi,
) or pod2usage(2);

use Data::Dumper;
print Dumper(\%flag_opts);

__END__

Example Session:
[PROMPT] topts.pl --flag one=something two="another thing" three=
$VAR1 = {
'one' => 'something',
'three' => '',
'two' => 'another thing'
};

[PROMPT] topts.pl --flag one=something two="another thing" three
Error: Invalid Arguments to flag


_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to