#!/usr/bin/perl -w
##########
# A couple of cool subroutines for parsing command line arguments.
# It's been done a thousand, thousand times but this is the first time I did it right.
##########
use Data::Dumper;
use strict;
use warnings;
sub splitArg {
my $str = shift;
return split(/=/, $str);
}
sub parseArgs {
my %args = ();
my $lastarg = "";
foreach my $arg (@ARGV) {
if (substr($arg, 0, 2) eq "--") {
my $temp = substr($arg, 2);
if ($arg !~ m/=/) {
$lastarg = $temp;
} else {
my ($k, $v) = splitArg($temp);
$args{$k} = $v;
}
}
elsif (substr($arg, 0, 1) eq "-") {
my $temp = substr($arg, 1);
if ($arg !~ m/=/) {
$lastarg = $temp;
} else {
my ($k, $v) = splitArg($temp);
$args{$k} = $v;
}
}
else {
if (length($lastarg) > 0) {
$args{$lastarg} = $arg;
$lastarg = "";
}
}
}
return %args;
}
my %args = parseArgs();
print Dumper(%args);
Comments