On 7/29/10 Thu  Jul 29, 2010  1:17 AM, "Sooraj S"
<soorajspadmanab...@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 < @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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to