Getopt::Long in perl

Hi,

I am using Getopt::Long to accept the command line arguments.

Logic
---------
I have two mandatory options.
1.mod1_num
2.mod2_num

Both of them can accept either a 3digit number or another parameter
"preserve" which inturn accepts a 3digit number.

eg: my_script -mod1_num 123 -mod2_num 234
my_script -mod1_num -preserve 123 mod2_num 234

If "preserve" is used with "mod1_num" it cannot be used with
"mod2_num" and viceversa.

How to do this with GetOptions? Please help me.


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sooraj S [ Do, 29 Juli 2010 10:17 ] [ ID #2045296 ]

Re: Getopt::Long in perl

On 7/29/10 Thu Jul 29, 2010 1:17 AM, "Sooraj S"
<soorajspadmanabhan [at] gmail.com> scribbled:

> Hi,
>
> I am using Getopt::Long to accept the command line arguments.
>
> Logic
> ---------
> I have two mandatory options.
> 1.mod1_num
> 2.mod2_num
>
> Both of them can accept either a 3digit number or another parameter
> "preserve" which inturn accepts a 3digit number.
>
> eg: my_script -mod1_num 123 -mod2_num 234
> my_script -mod1_num -preserve 123 mod2_num 234
>
> If "preserve" is used with "mod1_num" it cannot be used with
> "mod2_num" and viceversa.
>
> How to do this with GetOptions? Please help me.

The logic you want is unusual and will not be supported by Getopt::Long. I
believe you have two choices: 1) change the logic, 2) do the processing
yourself.

1. For more normal processing, combine the two options '-mod1_num -preserve'
into one (e.g., '-mod1_num_preserve') followed by the numerical argument.
This follows the normal logic supported by Getopt::Long.

2. If you can't change the logic, then process the command-line arguments
yourself with something like this (untested):

for( my $i = 0; $i < [at] ARGV; $i++ ) {
if( $ARGV[$i] eq '-mod1_num' ) {
if( $ARGV[$i+1] eq '-preserve ) {
$num = $ARGV[$i+2];
$i += 2;
}else{
$num = $ARGV[++$i];
}
}elsif( $ARGV[$i] eq '-mod2_num' ) {
...
}
}




--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Do, 29 Juli 2010 18:23 ] [ ID #2045297 ]
Perl » gmane.comp.lang.perl.beginners » Getopt::Long in perl

Vorheriges Thema: Monitor bandwidth of multiple servers viewable on single Web front
Nächstes Thema: Compare file/dir size