Adding options to data values is non-standard, in the extreme. You also appear to have multi-character options with only a single hyphen,
-input instead of
--input for example, which breaks the standard. Therfore I don't think any of the normal modules can help.
The only way I can see of doing this is to parse the argument list yourself. For example:
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";
Gives:
$VAR1 = {
'-r' => [
{},
'1000'
],
'-des' => [
{},
'finding_files'
],
'-input' => [
{
'-nc' => undef,
'-nd' => undef,
'-na' => undef
},
'data',
'modem',
'apps'
]
};
You can then check to see if options have been set by using
exists on the relevant hash.
Update: The problem with this though is that I am not setting the "sub-options" for the data items, for example -nd is duplicated. There is an inconsistency in your model so far as I can see. How do we know if 'data' should have options or not? Still, you might be able to develop my suggestion further, sicne you know what is valid and what is not.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.