I am trying to parse the command line using Getopt. Unfortunately, I cannot seem to
get it to work.I have looked over the relevant documentation, but still cant seem to
get it right.
Would appreciate any help in pointing out what I must be doing incorrectly. Also, the
Camel book ( page 446, 448) seem to indicate that the usage of the command line
options should be : --option_name ( ie two hyphens followed by the option name)
Is this correct ? Even using that I did not seem to get it to work correctly !
Below is the code , followed by the attempts ( unsuccesful), that I made to test if
the code works.
Thanks
AN
# Code Starts here
#!/usr/bin/perl -w
use strict;
## catch the program name
my $progname = $0;
$progname =~ s/^.*\///;
&GetCmdLine();
#######################
# sub : GetCmdLine
#######################
sub GetCmdLine {
my @script_args = @ARGV;
my $opt_w;
my $opt_cmdfile;
my $opt_help;
my $setup_w;
my $setup_cmd;
#parsing command line
use Getopt::Long;
$Getopt::Long::ignorecase = 1;
$Getopt::Long::autoabbrev = 1;
if(! GetOptions("-",
"w=s",
"cmdfile=s",
"help|h"
)){
&HelpRoutine;
print "$progname -E- 0x001 Command line error.\n";
exit(1);
}
if(defined($opt_help) && $opt_help){
&HelpRoutine;
exit(0);
}
if(defined($opt_w)){
$setup_w = "$opt_w";
print "sw : $setup_w\n";
}
if(defined($opt_cmdfile)){
$setup_cmd = "$opt_cmdfile";
print "cmd : $setup_cmd\n";
}
}
############################################
#Sub HelpRoutine
############################################
sub HelpRoutine {
print " Usage: $progname -w <work_area> -n <testcase name> -b <top_level_name>
-cmdfile <path to expander script> [
-w : work_area definition
-cmdfile <file> : <full path to location of actual script file>
-h | help : Print help message
";
} #end of printing help message
###
# Examples of Usage:
an>test.pl --h
Unknown option: -h
Usage: test.pl -w <work_area> -n <testcase name> -b <top_level_name> -cmdfile <path
to expander script> [
-w : work_area definition
-cmdfile <file> : <full path to location of actual script file>
-h | help : Print help message
test.pl -E- 0x001 Command line error.
an>test.pl -h
an>
an>test.pl -w da
an>
---------------------------------
Do you Yahoo!?
Y! Web Hosting - Let the expert host your web site