Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

multiple entries per option (Getopt)

by mlebel (Hermit)
on Jan 26, 2012 at 20:29 UTC ( #950214=perlquestion: print w/ replies, xml ) Need Help??
mlebel has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone,

I am wondering if there is a way to have multiple entries passed for a single option when launching a perl script. (the only post that I found this on was using getopt::long but i didn't quite understand the answer)

Here is an example of what I mean:

testmachine># ./testcode.pl -o entry1 entry2 or testmachine># ./testcode.pl -o entry1 -o entry2

If this is possible, what would be the code to handle this within a perl script?

If it is not possible, how could a situation like that be handled?

Any help is appreciated!

Thanks, Marc.

Comment on multiple entries per option (Getopt)
Download Code
Re: multiple entries per option (Getopt)
by toolic (Chancellor) on Jan 26, 2012 at 20:38 UTC
Re: multiple entries per option (Getopt)
by Perlbotics (Abbot) on Jan 26, 2012 at 20:51 UTC

    You could feed your examples to the following program and see what happens... The important part is the "o=s" telling Getopt to look for -o switches with a string argument and the array-ref telling it to accumulate the -o arguments into an array.

    use strict; use warnings; use Getopt::Long; my @o_list; unless ( GetOptions ("o=s" => \@o_list) ) { print "GetOptions had problems parsing ARGV!\n"; } print "List of -o things: ", join(", ", @o_list), "\n"; print "Unparsed arguments: ", join(", ", @ARGV ), "\n";

Re: multiple entries per option (Getopt)
by mlebel (Hermit) on Jan 27, 2012 at 02:41 UTC

    you guy's are awesome!

    Through both of your posts, I was able to achieve exacly what i needed. And with the added bonus that I understand what is happening.

    Thank you toolic and perlbotics!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://950214]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2014-09-22 04:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (178 votes), past polls