Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: -x accepts up to 3 additional arguments

by QM (Parson)
on Jun 23, 2005 at 15:31 UTC ( [id://469427]=note: print w/replies, xml ) Need Help??


in reply to -x accepts up to 3 additional arguments

Update: I noticed that I hadn't read the OP closely enough. I've reworked it to provide optional parameters. The exact specification is lacking in the OP, so this is my best guess (to date):

Then there's Getopt::Declare:

#!/your/perl/here use strict; use warnings; use Getopt::Declare; my $option_spec = qq{ # note: required tab between last argument and description -x <x0> [<x1> <x2> <x3>] -x takes 1 or 4 args [required] -y <y0> [<y1> <y2>] -y takes 1 or 3 args [required] -z <z0> [<z1>] -z takes 1 or 2 args [required] At least one of these options is required, and all are mutually ex +clusive. [mutex: -x -y -z] Examples: $0 -x zero one two three $0 -y zero alpha beta $0 -z zero last_but_not_least }; my $options = Getopt::Declare->new( $option_spec ) or die "\n**** Error processing command line options, terminating $0 +\n"; sub do_something { print "@_\n"; } if ( $options->{'-x'} ) { if ( defined( $options->{'-x'}{'<x1>'} ) ) { do_something("-x", $options->{'-x'}{'<x0>'}, $options->{'-x'}{'<x1>'}, $options->{'-x'}{'<x2>'}, $options->{'-x'}{'<x3>'} ) if $options->{'- +x'} } else { do_something("-x", $options->{'-x'}{'<x0>'} ); } } if ( $options->{'-y'} ) { if ( defined( $options->{'-y'}{'<y1>'} ) ) { do_something("-y", $options->{'-y'}{'<y0>'}, $options->{'-y'}{'<y1>'}, $options->{'-y'}{'<y2>'} ) if $options->{'- +y'} } else { do_something("-y", $options->{'-y'}{'<y0>'} ); } } if ( $options->{'-z'} ) { if ( defined( $options->{'-z'}{'<z1>'} ) ) { do_something("-z", $options->{'-z'}{'<z0>'}, $options->{'-z'}{'<z1>'} ) if $options->{'- +z'} } else { do_something("-z", $options->{'-z'}{'<z0>'} ); } }
Which gives these results:
C:\Perl\perl\perlmonks>469375.pl -y zero -y zero C:\Perl\perl\perlmonks>469375.pl -y zero one two -y zero one two C:\Perl\perl\perlmonks>469375.pl -z zero one -z zero one C:\Perl\perl\perlmonks>469375.pl -x zero -x zero C:\Perl\perl\perlmonks>469375.pl -x zero one two three -x zero one two three C:\Perl\perl\perlmonks>469375.pl -y zero -y zero C:\Perl\perl\perlmonks>469375.pl -y zero alpha beta -y zero alpha beta C:\Perl\perl\perlmonks>469375.pl -z zero -z zero C:\Perl\perl\perlmonks>469375.pl -z zero last_but_not_least -z zero last_but_not_least C:\Perl\perl\perlmonks>469375.pl -x zero -y zero -z zero Error: parameter '-y' not allowed with parameter '-x' Error: parameter '-z' not allowed with parameter '-x' (try 'C:\Perl\perl\perlmonks\469375.pl -help' for more information) **** Error processing command line options, terminating C:\Perl\perl\p +erlmonks\469375.pl

-QM
--
Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-25 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found