Hi i am a newbie to perl..

I have a script which copies files from local machine to a remote machine. I am using Getopt::Long to get the command line arguments.

"-d" : for directories
"-f" : for files

doubt #1

GetOptions ('d:s' => \@dir, 'f:s' => \@fil);

If i use this i am able to pass values like this - ./my_script.pl -d /home -d /etc
But i am not able to pass multiple values in a shot like - ./my_script.pl -d /home /etc

doubt#2

GetOptions ('d:s{,}' => \@dir, 'f:s{,}' => \@fil);

This is not even compiling and it says..

Error in option spec: "d:s{,}
Error in option spec: "f:s{,}

doubt#3
How to add one more option to rename directories..This Option should be enabled only if "-d" is specified.
"-r" : rename (only if directory)

Please guide me.

I like to use a different GetOpt

use Getopt::Std;
getopts("f:d:r");
my $file=$opt_f;
my $dir=$opt_d;
my $ren_flag=1 if($opt_r);

Would that work for you?

Regarding doubt #3, I think you would have to write conditional logic to allow the -r (rename) parameter only if the -d (directory) parameter is specified. Maybe roughly like the following:

#!/usr/bin/perl
#getopt01.pl
use strict;
use warnings;

use Getopt::Long;
 
my ($help, @dir, @rename_to);
 
#-- prints usage if there is an unknown
#   parameter or help option is passed
usage() if ( ! GetOptions('help|?' => \$help, 'd:s' => \@dir, 'r:s' => \@rename_to)
         or defined $help );

if (@rename_to > 0 and @dir < 1){
    die "-r option specified without -d option.\n"
        . "If -d not specified, -r is not allowed.\n"
        . "If you don't eat your meat, you can't have any pudding!\n"
}

sub usage
{
  print "Unknown option: @_\n" if ( @_ );
  print "usage: program [-d DIRECTORY_NAME] [-r NEW_DIRECTORY_NAME] [--help|-?]\n";
  exit;
}

Thanks...can u make me clear about doubt#1 and #2 also?

I'm not sure I understand doubts number 1 and 2. You say that GetOptions ('d:s{,}' => \@dir, 'f:s{,}' => \@fil); doesn't compile? The following works for me.

#!/usr/bin/perl
#getopt01.pl
use strict;
use warnings;

use Getopt::Long;
my (@dir, @fil);

#-- prints usage if there is an unknown parameter

usage() if ( ! GetOptions('d:s{,}' => \@dir, 'f:s{,}' => \@fil));
            
if (@dir > 0){
    print "\n-d option specified with the following values:\n"
        . join("\n", @dir), "\n\n";
}

if (@dir > 0){
    print "-f option specified with the following values:\n"
        . join("\n", @fil), "\n\n";
}

print "My perl version is: $]\n";
sub usage
{
  print "Unknown option: @_\n" if ( @_ );
  print "usage: program [-d DIRECTORY_NAME [DIRECTORY_NAME]...] [-f FILE_NAME [FILE_NAME]...]\n";
  exit;
}

Running the above on the command line:

$ perl getopt02.pl -d /home /etc -f note.txt appointments.txt list.com

-d option specified with the following values:
/home
/etc

-f option specified with the following values:
note.txt
appointments.txt
list.com

My perl version is: 5.010000

Hi

I copied your program and executed. But it errors out as before. Is it because of my perl version,which is 5.008 ?

I think the problem was with the version itself. I downloaded the new version of Getopt::Long and now its working fine. Thanks for your support, all.

That's great. I didn't think of that (Getopt:Long version.) Please mark this thread solved (even though it was you that solved it. :))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.