| test | code |
|---|---|
| test si il n'y a pas d'argument | if ($#ARGV == 0) { print "pas d'argument\n"; exit ; }
|
| test si il n'y a pas le bon nombre d'arguments |
if ($#ARGV != 0) {
print "usage: findfile filename\n";
exit;
}
$filename=ARGV[0];
|
|
test si il y a pas le bon nombre d'arguments (ici 2) |
if ($#ARGV == 1) {
$INFILE= $ARGV[0];
$OUTFILE= $ARGV[1];
}
else
{
print "usage: convert infile outfile\n";
exit;
}
|
| teste le nombre d'arguments |
if ( @ARGV > 0 )
{
print "Number of arguments: " . scalar @ARGV . "\n";
}
else
{
print "No arguments!\n";
}
|
| fournit l'aide si le premier argument est -h |
if ($ARGV[0] eq "-h") {
print " syntaxe : script \n" ;
exit(-1) ;
} ; # fin
|
| fournit l'aide si le premier argument est -h |
if ($ARGV[0] eq "-h") {
print " syntaxe : script \n" ;
exit(-1) ;
}
|
| le script attend l'option -h ou aucun argument |
if ( ($ARGV[0] eq "-h" ) || ( $#ARGV != 1) ) {
print " syntaxe : script \n" ;
exit(-1) ;
}
|
| test si il y a un nombre d'arguments définis |
use Getopt::Long;
my %args;
GetOptions(\%args,
"arg1=s",
"arg2=s",
"arg3=s",
) or die "Invalid arguments!";
die "Missing -arg1!" unless $args{arg1};
die "Missing -arg2!" unless $args{arg2};
die "Missing -arg3!" unless $args{arg3};
|
| le script sort si il n'y a pas au moins un argument | #!/usr/bin/perl die "$0 a besoin d au moins un argument argument.\n" if $#ARGV < 0 ; print "@ARGV\n"; print "$ARGV[0]\n"; print "$ARGV[1]\n"; print "il y a ", $#ARGV + 1," arguments.\n"; print "le dernier est $ARGV[$#ARGV].\n"; |
| affiche l'usage si il n'y a pas de parametres passés où si ils sont inconnus |
use Getopt::Long;
my ($help, @url, $size);
usage() if ( @ARGV < 1 or
! GetOptions('help|?' => \$help, 'url=s' => \@url, 'size=i' => \$size)
or defined $help );
sub usage
{
print "Unknown option: @_\n" if ( @_ );
print "usage: program [--url URL] [--size SIZE] [--help|-?]\n";
exit;
}
|
test des arguments en mode court avec la fonction getopt.
|
#!/usr/local/bin/perl -w
use 'Getopt::Std'
getopt('abcd:ef:');
print "Switch a est on\n" if $opt_a;
print "Switch b est on \n" if $opt_b;
print "Switch c est on \n" if $opt_c;
print "Debug switch mis à $opt_d\n" if $opt_d > 5;
print "Switch e est on\n" if $opt_e;
if ($opt_f) {
print "le fichier $opt_f ne peut être trouvé\n" unless -e $opt_f;
}
for $I (1..5) {
print "Valeur de I est $I\n" if $opt_d;
}
|
| test les options passées en arguments avec la fonction getopt en mode long. |
use Getopt::Long;
my $ret = GetOptions ("f|filename:s");
my $filename = $opt_f || die "Usage: $0 -f filename\n";
open (INPUT, "$filename") || die "Could not open file $filename : $!\n";
while (<INPUT>)
{
chop;
print "Line $. = <$_>\n";
}
close (INPUT);
|
| exemple d'arguments parsé avec usage |
#!/usr/bin/perl
use strict;
use Getopt::Long;
# valeurs par defaut
my $name = 'John';
my $age = 25;
my $employed = 0;
my $help = 0;
GetOptions(
'nom=s' => \$name,
'age=i' => \$age,
'employe!' => \$employed,
'help!' => \$help,
) or die "Usage incorrect!\n";
if( $help ) {
print " -nom=john -age=25 -employe\n";
print " -nom=doe -age=40\n";
} else {
print "nom: $name.\n";
print "age: $age.\n";
print "status : " . ($employed ? ' ' : 'pas ') . "employé.\n";
}
|
comment lire un fichier de configuration. Le format du fichier est de type:
#-------------# # propriétés # #-------------# $logr_dir = '/var/opt/log/app.log'; $checkmode = 'true'; $max_value=120; | my $config_file = $ARGV[0]; open CONFIG, "$config_file" or die "Program stopping, couldn't open the configuration file '$config_file'.\n"; my $config = join "", |