my $argv = {};
for( my @tmp_ARGV = @ARGV; my $arg = shift @tmp_ARGV; ) {
if(0) {}
elsif( $arg eq "-short" ) {
$argv->{test_length} = 'short';
}
elsif( $arg eq "-full" ) {
$argv->{test_length} = 'short';
}
else {
die "unrecognized command line argument: <$arg>";
}
}
I rather like creating a hash of options,
but if you prefer variables:
my $short;
for( my @tmp_ARGV = @ARGV; my $arg = shift @tmp_ARGV; ) {
if(0) {}
elsif( $arg eq "-short" ) {
$short = 1;
}
elsif( $arg eq "-full" ) {
$short = 0;
}
else {
die "unrecognized command line argument: <$arg>";
}
}
This can be made extensible, by calling subfunctions to parse, so-called argv-recognizers. These can recognize individual options, or parse a long way.
my $short;
for( my @tmp_ARGV = @ARGV; my $arg = shift @tmp_ARGV; ) {
if(0) {}
elsif( $arg eq "-short" ) {
$short = 1;
}
elsif( argv_parser1(\@tmp_ARGV) {}
elsif( argv_parser2(\@tmp_ARGV) ) {}
else {
die "unrecognized command line argument: <$arg>";
}
}
The following may be more uniform:
for( my @tmp_ARGV = @ARGV; my $arg = @tmp_ARGV[0]; shift @tmp_ARGV ) {
...
}
---
Why use this instead of Getopts? Getopts::Lomng? Etc.
Mainly
a) Getopts does not compose modules with different parsers
b) the above is trivial, easy to remember, and can be extended to handle any different argument syntax.