[EMAIL PROTECTED] wrote: > Before I roll my own ARGV parse code I wanted to know if someone had > some sample code to get me started. I just want to be able to pass > simple switches that contain one parameter each to my perl app. I have > looked to the list first for I plan to accept the switches in any > order, but only allow each of them to be called once each. The only > important dependencies that I can think of right now is that switch - > cmd has to be passed if any other switch is used and all switches must > have a valid parameter. I'm not asking anyone to write me anything, > but any related sample code will help tremendously. > > ex: perlscript -cmd show -type session -loc all > ex: perlscript -type process -cmd show
First: Don't piggy-back on someone else's post. Create a new thread (just copy the email address). You can always use one of the getopt modules out there, but I'm happy with my own little invention (untested likeness in this post) : use strict; use warnings; our %A; # get commandline switches into %A for (my $ii = 0; $ii < @ARGV; ) { last if $ARGV[$ii] =~ /^--$/; if ($ARGV[$ii] !~ /^-{1,2}(.*)$/) { $ii++; next; } my $arg = $1; splice @ARGV, $ii, 1; if ($arg =~ /^([\w]+)=(.*)$/) { $A{$1} = $2; } else { $A{$1}++; } } # The syntax is slightly different for you (note the ='s) : # ex: perlscript -cmd=show -type=session -loc=all # ex: perlscript -type=process -cmd=show # Then I do a usage like this: (my $prog = $)) =~ s/^.*[\\\/]//; my $usage << EOD; Usage: $prog [-cmd=<cmd>] [-type={x|y|z}] [-loc=<loc>] -cmd=<cmd> <cmd> is the command to execute {show|tell|etc} -cmd is a mandatory arg (def: show) -loc=<loc> <loc> is the full path to the widget factory (def: C:/widget) -type={x|y|z} Widget type to use (def: x) EOD die $usage if ${h} or $A{help} or not $A{cmd}; # Then I usually handle them like: my $cmd = $A{cmd} || 'show'; # command to execute my $loc = $A{loc} || 'C:/widget'; # widget location my $type = ${type} || 'x'; # type of widget # You could easily handle multiple switches of the same name by # either appending them with | bars or using an anonymous array: $A{$1} .= "|$2" if exists $A{$1}; # append the next value after | # or not as usable for me: push @{$A{$1}}, $2; # in this case all args would be arrays # unless you had a type hash to determine # arg type (scalar|array) _______________________________________________ Perl-Unix-Users mailing list Perl-Unix-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs