Problem checking if a command line option has been specified using Getopt::Long 
Author Message
 Problem checking if a command line option has been specified using Getopt::Long

Hi.

I am having some trouble finding a method to check if a
command line option which take an optional argument has been
specified on the script's command line.  I am using
Getopt::Long with PERL 5.005_03 on Solaris 7.

I have written a script called "run_test" with options
-t <testname1> and -diag [testname2].

-t is a required option which takes in a required argument
and sets it to $testname1.
-diag is an optional switch which takes in an optional
argument and sets it to $testame2.

I would like to run the script as follows:
1) runtest -t test1
2) runtest -t test1 -diag
3) runtest -t test1 -diag test2

In (1), I would like to set $testname1 and $testname2 to
"test1", but flag that -diag has not been specified on the
command line.
In (2), I would like to set $testname1 and $testname2 to
"test2" and flag that -diag has been specified on the
command line but without an argument.
In (3), I would like to set $testname1 to "test1" and
$testname2 to "test2" and flag that -diag has been specified
on the command line.

Here is how I am implementing the mapping between options
and arguments:

    $result = GetOptions("t:s" => \$testname1,
    "diag:s" => \$testname2);

From the documentation of Getopt::Long I can't figure out a
mechanism to achieve my requirements above.  For instance,
if I don't specify the -diag switch and just call
GetOptions(), $testname2 will be assigned the null string ""
.  If I specify -diag without an argument, $testname2 will
still be assigned the null string.  If I specify -diag with
its optional argument, then everything is ok.

Is there a workaround to my problem?  I would really like to
use a package which accepts long command line options but I
am not proficient enough in PERL to do so myself yet.

Thanks for your help.

Nestor



Sun, 09 May 2004 07:16:06 GMT  
 Problem checking if a command line option has been specified using Getopt::Long

exist...

Quote:
> I am having some trouble finding a method to check if a
> command line option which take an optional argument has been
> specified on the script's command line.  I am using
> Getopt::Long with PERL 5.005_03 on Solaris 7.

Getopt::Long implements the GNU syntax rules for command line switch
processing.

If you want your script to use some other incompatible rules for
interpreting the command line you'll have to roll your own or find
another module.

Quote:
> I have written a script called "run_test" with options
> -t <testname1> and -diag [testname2].

Sorry that command syntax does not conform to the GNU rules.

Quote:
> -t is a required option which takes in a required argument
> and sets it to $testname1.
> -diag is an optional switch which takes in an optional
> argument and sets it to $testame2.

In GNU long options are introduced with a double dash.  This has
nothing to do with Perl.


Wed, 12 May 2004 01:43:59 GMT  
 Problem checking if a command line option has been specified using Getopt::Long

Quote:

> I am having some trouble finding a method to check if a
> command line option which take an optional argument has been
> specified on the script's command line.  I am using
> Getopt::Long with PERL 5.005_03 on Solaris 7.

> I have written a script called "run_test" with options
> -t <testname1> and -diag [testname2].

Please disregard my previous massage - I was talking rubbish.

BTW: This newsgroup still doesn't exist.



Wed, 12 May 2004 01:50:00 GMT  
 Problem checking if a command line option has been specified using Getopt::Long

Quote:

>     $result = GetOptions("t:s" => \$testname1,
>     "diag:s" => \$testname2);
> I don't specify the -diag switch and just call
> GetOptions(), $testname2 will be assigned the null string ""

I am unable to reproduce this bug, admittedly in a later version of
Perl from the one you are using.  Are you sure there's not something
else in your program that may be setting $testname2.

BTW: This newsgroup still doesn't exist.



Wed, 12 May 2004 01:55:20 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. ANNOUNCE: Getopt::Long 2.9 -- Universal command line options processing

2. ANNOUNCE: Getopt::Long 2.6 -- Universal command line options processing

3. ANNOUNCE: Getopt::Long 2.9 -- Universal command line options processing

4. ANNOUNCE: Getopt::Long 2.6 -- Universal command line options processing

5. Using Getopt::Long with multiple options in quotes

6. Getopt::Long, using '?' as option

7. Getopt:Long arguments that are not (options or option values)

8. howto specify command line options

9. Getopt::Regex : Regular expression command line options

10. check command line options?

11. Getopt::Long and Options in single quotes?

12. GetOpt::Long - Corequisite options?

 

 
Powered by phpBB® Forum Software