Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

get array from getoptions

by Utrecht (Novice)
on Aug 19, 2008 at 16:29 UTC ( [id://705257]=perlquestion: print w/replies, xml ) Need Help??

Utrecht has asked for the wisdom of the Perl Monks concerning the following question:

Hello I'm quite new to perl, but I have some code already that I need to adapt. The problem is that I don't know how to obtain an array using Getopt::Long My code is the following:
use Getopt::Long ; my $input = {output_type => 'default'} ; my @input_array = {output_type => 'default'} ; my $p = new Getopt::Long::Parser ; $p->configure('pass_through') ; my $getoptret = $p->getoptions( + 'files|f:s' =>\@input_array->{files}, #filename(s) 'eventlist|e:s' =>\$input->{eventlist}, #name of the eve +ntlist 'output_directory|o:s' =>\$input->{output_directory}, # desire +d output directory ); die "Error: Give input SEED volume\n" if (! -e @input->{files}) ; die "Error: Give name of eventlist that should be used\n" if (! $input +->{eventlist}) ; die "Error: Give output directory\n" if (! $input->{output_directory}) + ;
My question: How can I parse an array by executing the filename with the option "-f test_1 test_2"
The error message I get is:
"No such pseudo-hash field "files" at extract_mini-SEED line 24"
This is the line where my $getoptret gets filled. I hope someone has an idea. Thank you in advance.

Replies are listed 'Best First'.
Re: get array from getoptions
by ctilmes (Vicar) on Aug 19, 2008 at 16:46 UTC
    First:, add "use strict; use warnings;" to the top.

    Getopt::Long doesn't handle something like "-f test_1 test_2". It will handle "-f test_1 -f test_2" or "-f test_1,test_2" (you do the split), or even "-f 'test_1 test_2'" (again, split after you get the args).

    Alternately, you can have it treat -f as a boolean, then grab the filenames test_1 and test_2 from @ARGV after Getopt::Long has done its thing.

Re: get array from getoptions
by psini (Deacon) on Aug 19, 2008 at 18:48 UTC

    Disclaimer: I never tried this feature that is declared "experimental", so I really don't know if it works at all.

    From Getopt::Long POD:

    Warning: What follows is an experimental feature.
    Options can take multiple values at once, for example
    --coordinates 52.2 16.4 --rgbcolor 255 255 149
    This can be accomplished by adding a repeat specifier to the option specification. Repeat specifiers are very similar to the {...} repeat specifiers that can be used with regular expression patterns. For example, the above command line would be handled as follows:
    GetOptions('coordinates=f{2}' => \@coor, 'rgbcolor=i{3}' => \@color);
    The destination for the option must be an array or array reference.
    It is also possible to specify the minimal and maximal number of arguments an option takes. foo=s{2,4} indicates an option that takes at least two and at most 4 arguments. foo=s{,} indicates one or more values; foo:s{,} indicates zero or more option values.

    So, if I understand correctly, an option like:

    'files|f:s{,}' => \@input_array->{files}

    could work as you expected.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: get array from getoptions
by ctilmes (Vicar) on Aug 19, 2008 at 16:59 UTC
    With the caveat that I'm really sure exactly what you are doing, I reformatted and tweaked a bit:
    use strict; use warnings; use Getopt::Long; my $input = { output_type => 'default', files => [], eventlist => [] }; my $p = new Getopt::Long::Parser; $p->configure('pass_through'); my $getoptret = $p->getoptions ( 'files:s@' => $input->{files}, #filename(s) 'eventlist:s@' => $input->{eventlist}, #name of the e +ventlist 'output_directory:s' => \$input->{output_directory}, # desired out +put dir ); die "Error: Give input SEED volume\n" unless defined $input->{files}; foreach (@{$input->{files}}) { die "Error: Missing file" unless -e; } die "Error: Give name of eventlist that should be used\n" unless defined $input->{eventlist}; die "Error: Give output directory\n" unless defined $input->{output_directory}; print "File List: @{$input->{files}}\n"; pritn "Event List: @{$input->{eventlist}}\n"; print "Output Directory: $input->{output_directory}\n";
    If you run it like this:

    perl testit.pl -f test_1 -f test_2 -e event1 -e event2 -o outputdir

    it will output this:

    File List: test_1 test_2 Event: event1 event2 Output Directory: outputdir

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-18 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found