in reply to (Continuation ....)Dynamic option
Gives:use warnings; use strict; # findfiles -input [-nd -na ] data [-nc -nd] modem apps -des "finding +files" -r 1000 # "finding files" altered to finding_files for test purposes our @ARGV = qw(-input -nd -na data -nc -nd modem apps -des finding_fil +es -r 1000); use Data::Dumper; use GetOpt::Std; my %args = ('-input' => [{}], '-des' => [{}], '-r' => [{}]); my $active_arg = 0; for my $arg (@ARGV) { if (substr($arg,0,1) eq '-') { if (exists $args{$arg}) { $active_arg = $arg } elsif ($active_arg) { $args{$active_arg}[0]{$arg} = undef } else { die "Invalid argument(1): $arg" } } else { if ($active_arg) { push @{$args{$active_arg}},$arg } else { die "Invalid argument(2): $arg" } } } print Dumper(\%args),"\n";
You can then check to see if options have been set by using exists on the relevant hash.$VAR1 = { '-r' => [ {}, '1000' ], '-des' => [ {}, 'finding_files' ], '-input' => [ { '-nc' => undef, '-nd' => undef, '-na' => undef }, 'data', 'modem', 'apps' ] };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: (Continuation ....)Dynamic option
by Anonymous Monk on Mar 16, 2011 at 12:31 UTC |