For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2007 > GetOptions argument given multiple times









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author GetOptions argument given multiple times
Fredrik Y Andersson

2007-07-23, 6:59 pm

Hi
I'm trying to use multiple options with GetOptions

$>perl myprogram.pl --byggvy=abc --byggvy=def

How do you write the code for the GetOptions?
Best Regards
Fredrik Andersson

My code:

#!/usr/local/bin/perl
use warnings;
use Getopt::Long;

my %Options = ();

#Is this the correct way?
GetOptions(\%Options,
"edition=s" => \$edition,
"leveransdata=s" => \$leveransdata,
"help" => \$help,
"byggvy=s" => \@byggvy);

#Or is this the correct way?
#GetOptions(\%Options, "help", "edition=s", "byggvy=s@",
"leveransdata=s");

if ($Options{byggvy}) {
print "BYGGVY: $Options{byggvy}\n";
# Code for byggvy here
# I would like to have below code here [ii] but can't get this code to
work either
#I run the program like this: $>perl myprogram.pl --byggvy=abc --
byggvy=def
# How do I get the GetOptions data to the foreach loop?
# There is a directory called abc and def
} elsif ($Options{edition}) {
print "EDITION: $Options{edition}\n";
# Code for edition here
} elsif ($Options{leveransdata}) {
print "LEVERANSDATA: $Options{leveransdata}\n";
# Code for leveransdata here
} elsif ($Options{help}) {
print "HELP: $Options{help}\n";
} else {
print "HELP2: Show help text again\n";
}


#[ii]
use File::Copy;
foreach (@byggvy) {
$apexvy=$_;
system ("cp ./$apexvy/*.lfm /net/users/esbfran/XLEV/."); #or die
"Can't copy file $apexvy_wrk/*.lfm: $!";
system ("cp ./$apexvy/*.lfl /net/users/esbfran/XLEV/."); #or die
"Can't copy file $apexvy_wrk/*.lfm: $!";
system ("tar -cvf /net/users/esbfran/XLEV/$apexvy.tar $apexvy_wrk >/
dev/null"); #or die "Can't tar: $!";
}

Chas Owens

2007-07-23, 6:59 pm

On 7/23/07, fredrik.y.andersson@saabgroup.com
<fredrik.y.andersson@saabgroup.com> wrote:
> Hi
> I'm trying to use multiple options with GetOptions

snip


Both of the ways specified in the manual work for me:

#!/usr/bin/perl

use warnings;
use strict;

use Getopt::Long;

@ARGV = qw<--library foo --library bar>;

my @libfiles;

GetOptions ("library=s" => \@libfiles);

print map { "[$_]\n" } @libfiles;

@ARGV = qw<--library foo --library bar>;

my $aref;

GetOptions ("library=s@" => \$aref);

print map { "[$_]\n" } @$aref;
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com