FAQ
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.

Search Discussions

  • Jim Gibson at Jul 29, 2010 at 4:23 pm
    On 7/29/10 Thu Jul 29, 2010 1:17 AM, "Sooraj S"
    <soorajspadmanabhan@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' ) {
    ...
    }
    }

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJul 29, '10 at 8:18a
activeJul 29, '10 at 4:23p
posts2
users2
websiteperl.org

2 users in discussion

Sooraj S: 1 post Jim Gibson: 1 post

People

Translate

site design / logo © 2021 Grokbase