Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

probleme with getopt

Status
Not open for further replies.

donny750

Programmer
Joined
Jul 13, 2006
Messages
145
Location
FR
hello,

I use getopt but i've 2 variables begin with the same character;
It is possible with getopt to choose another character;
I explain
i've this variables
infile
inputfile
can i use the character "n" for the variable iNputfile
si i call my script like this
test.pl -i a.txt -n b.txt ?

Code:
use Getopt::Long;
  my $infile   ;
  my $inputfile;
  
  $result = GetOptions ("infile=s" => \$infile,   
                        "inputfile=s"   => \$data,      
                         );

thanks
 
Straight from the documentation:
Options with multiple names

Often it is user friendly to supply alternate mnemonic names for options. For example --height could be an alternate name for --length. Alternate names can be included in the option specification, separated by vertical bar | characters. To implement the above example:

GetOptions ('length|height=f' => \$length);

The first name is called the primary name, the other names are called aliases. When using a hash to store options, the key will always be the primary name.

Multiple alternate names are possible.

So therefore, you have:

Code:
use Getopt::Long;
my $infile;
my $inputfile;
  
$result = GetOptions ("infile=s" => \$infile,   
                      "inputfile[COLOR=green]|n[/color]=s"   => \$data,      
                       );
 
Also, please note that you currently have the inputfile parameter bound to "$data" instead of "$inputfile". This is probably a bug.
 
thanks Miller i've seen,
i modify it
 
just a question
if i use a variable like a flag it's impossible to recover its value ??

I've try this
Code:
  >myscript.pl -v test

but i've 1 and not "test";

Code:
#!/usr/bin/perl

use Getopt::Long;

 
  my $verbose; 
  
  $result = GetOptions ("verbose"  => \$verbose);  # flag

if($verbose){

print "ok\n";
}
else
{
print "not ok\n";

}

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top