Perl Getopt::Long question - stopping multiple args


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Getopt::Long question - stopping multiple args
# 1  
Old 02-17-2010
Perl Getopt::Long question - stopping multiple args

Hi there, I have an example basic script (below) and ive been trying to figure out how to stop multiple arguments to my options occuring. for example using the example script below I can issue two arguments for, say the --surname option and it will not barf at me (although thats what i want it to do). It just ignores the second argument to the --surname option completely and happily continues.

Code:
#./name.pl --firstname John --surname Smith Jones
firstname is set to  - John
surname is set to  - Smith

How would i get it to barf at me for providing too many options ?

Any help would be great



The Script

Code:
#!/bin/perl

use strict;
use Getopt::Long;

my $firstname = "";
my $surname = "";
my $help = "";

GetOptions (
                "firstname=s"           => \$firstname,
                "surname=s"             => \$surname,
                "help!"                 => \$help,

                ) or die "Incorrect usage.\n";


if( $help )
{
        print "Common on, it's really not that hard.\n";
} else {



        if (!$firstname) { print "You have failed to enter a --firstname value\n"; exit 11 }
        if (!$surname) { print "You have failed to enter a --surname value\n"; exit 12 }


        print "firstname is set to  - $firstname\n";
        print "surname is set to  - $surname\n";

}

# 2  
Old 02-17-2010
Getopt::Long won't stop on any usage that isn't explicitly forbidden by it's internals. One way would be to allow multiple values for Getopt::Long, and throw an error yourself if the resulting array contains more than 1 value.

---------- Post updated at 14:55 ---------- Previous update was at 14:53 ----------

Or define your own subroutine handler for that option.
# 3  
Old 02-17-2010
Thanks Pludi, Ive been playing around and i wonder if you could help me clarify something. I changed the relevant lines (bolded in red below) to make surname an array (which looks like it creates a reference to the array). but it still seems to print only the one argument ?

Am i missing something here? Will a scalar reference to an array always only return the first object in an array ? apologies if ive done something dumb here

Code:
#!/bin/perl
use strict;
use Getopt::Long;

my $firstname = "";
my $surname = "";
my $help = "";

GetOptions (
                "firstname=s"           => \$firstname,
                "surname=s@"             => \$surname,
                "help!"                 => \$help,

                ) or die "Incorrect usage.\n";

if( $help )
{
        print "Common on, it's really not that hard.\n";
} else {

        print "firstname is set to  - @$firstname\n";
        print "surname is set to  - $surname\n";

}

As you can see its still the same

Code:
# ./name.pl --firstname John --surname smith jones    
firstname is set to  - John
surname is set to  - smith

I tried it using this as well
Code:
"surname=s"             => \@surname

...
...

print "surname is set to  - @surname\n";

with the same output, so maybe its not just an issue with references
# 4  
Old 02-17-2010
Good thing they provide an example with the documentation.
Code:
#!/bin/perl
use strict;
use Getopt::Long;

my $firstname = "";
my @surname   = ();
my $help      = "";

GetOptions(
    "firstname=s" => \$firstname,
    "surname=s@"  => \@surname,
    "help!"       => \$help,

) or die "Incorrect usage.\n";

if ($help) {
    print "Common on, it's really not that hard.\n";
}
else {

    print "firstname is set to  - $firstname\n";
    print "surname is set to  - @surname\n";

}

Code:
$ perl getopt.pl --firstname John --surname Smith --surname Jones
firstname is set to  - John
surname is set to  - Smith Jones

# 5  
Old 02-17-2010
Sorry Pludi, i think you may have misunderstood my problem, issuing multiple --surnames is not really the problem, its when they issue multiple arguments to the same --surname option

using the exact script you posted above, i issued

Code:
 # ./name.pl --firstname john --surname smith jones
firstname is set to  - john
surname is set to  - smith

wjen we print the array at the bottom of the script, the second argument is not printed
# 6  
Old 02-17-2010
Ah, you mean like the example a few paragraphs down in the documentation?
Code:
#!/bin/perl
use strict;
use Getopt::Long;

my $firstname = "";
my @surname   = ();
my $help      = "";

GetOptions(
    "firstname=s"   => \$firstname,
    "surname=s{1,}" => \@surname,
    "help!"         => \$help,

) or die "Incorrect usage.\n";

if ($help) {
    print "Common on, it's really not that hard.\n";
}
else {

    print "firstname is set to  - $firstname\n";
    print "surname is set to  - @surname\n";

}

Code:
$ perl getopt.pl --firstname John --surname Smith Jones
firstname is set to  - John
surname is set to  - Smith Jones

# 7  
Old 02-18-2010
Pludi, I apologise. I must have read through that documentaion 3 times but seem to have been overwhelmed by the information and missed the paragraph that i needed... Thank you for your help

---------- Post updated at 11:12 AM ---------- Previous update was at 06:14 AM ----------

Bah, would you believe it the "option=s{'1,}" functionality is only available in 5.10 by the looks of it, all my systems are perl 5.8 or less .

I get this error when running the script on 5.8 or below

Code:
# ./name.pl  
Error in option spec: "surname=s{1,}"

Smilie oh well
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question about getopts optional argument [args...]

There are many places where I can see the syntax description for optargs, which, usually boils down to this: getopts OPTSTRING VARNAME where: OPTSTRING tells getopts which options to expect and where to expect arguments VARNAME tells getopts which shell-variable to use for option reporting... (2 Replies)
Discussion started by: sharkura
2 Replies

2. Shell Programming and Scripting

Store args passed in array but not the first 2 args

Store args passed in array but not the first 2 args. # bash declare -a arr=("$@") s=$(IFS=, eval 'echo "${arr}"') echo "$s" output: sh array.sh 1 2 3 4 5 6 1,2,3,4,5,6 Desired output: sh array.sh 1 2 3 4 5 6 3,4,5,6 (2 Replies)
Discussion started by: iaav
2 Replies

3. Shell Programming and Scripting

Perl :: Getopt::Long in the program

While going through some of the perl script... I had found the below line.. use Getopt::Long; my $GetOptionsReturnCode = GetOptions ( '<>' => sub { push(@unknownArg, @_); }, 'h|help' => sub { &helpMessage(); exit 0; }, ); Could anyone please explain the above one ... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Programming

Perl Getopt::Long

Hi All I am using Getopt::Long in perl and i am trying to have it so if i dont supply a switch after the progname is will do a defult option i have the following GetOptions($OPT, 'debug|d', 'mail|m', ) or info(); i want it run the debug if it is not given a switch ... (1 Reply)
Discussion started by: ab52
1 Replies

5. Shell Programming and Scripting

using getopt for both short and long options

Hi , I am using getopt for both short and long options as below SHORTOPTS="a:c" LONGOPTS="alpha:,charlie" OPTS=$(getopt -o $SHORTOPTS --longoptions $LONGOPTS -n "$progname" -- "$@") eval set -- "$OPTS" while ; do case $1 in -a|--alpha) echo "-a or --alpha... (1 Reply)
Discussion started by: padmisri
1 Replies

6. Shell Programming and Scripting

using getopt for both short and long options

Hi , I am using getopt for both short and long options as below SHORTOPTS="a:c" LONGOPTS="alpha:,charlie" OPTS=$(getopt -o $SHORTOPTS --longoptions $LONGOPTS -n "$progname" -- "$@") eval set -- "$OPTS" while ; do case $1 in -a|--alpha) echo "-a or --alpha... (1 Reply)
Discussion started by: padmisri
1 Replies

7. Shell Programming and Scripting

using getopt for both short and long options

Hi , I am using getopt for both short and long options as below SHORTOPTS="a:c" LONGOPTS="alpha:,charlie" OPTS=$(getopt -o $SHORTOPTS --longoptions $LONGOPTS -n "$progname" -- "$@") eval set -- "$OPTS" while ; do case $1 in -a|--alpha) echo "-a or --alpha... (0 Replies)
Discussion started by: padmisri
0 Replies

8. Shell Programming and Scripting

getopt in perl

Hi, I have a perl script with two functions say func a and func b. sub a { ----------- --------- } sub b { --------- --------- } I want to use this function on command line as we can do in shell script using getopt. My motto here is to run the script like this ... (7 Replies)
Discussion started by: namishtiwari
7 Replies

9. AIX

Stopping multiple process on AIX

I'm trying to update a shared library (*.so) in our AIX machine. However, when I tried to delete the old *.so file, I get this error -> Cannot open or remove a file containing a running program. Based on the information I gather from the net, shared libraries are not unloaded (the file remains... (3 Replies)
Discussion started by: soulfactory2002
3 Replies

10. Programming

question about getopt()

I'm using getopt() to get command line options.One the optons accepts and argument.The argument is and offset.I was wondering how can I scecify that it's argument is of the type off_t.I've something like this "offset=(off_t)optarg" and it don't work. (1 Reply)
Discussion started by: angelfly
1 Replies
Login or Register to Ask a Question