Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!rpi!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!haven!uvaarpa!mmdf From: kayvan@mrspoc.transact.com (Kayvan Sylvan) Newsgroups: comp.lang.perl Subject: An improvement to getopts.pl Message-ID: <2636@uvaarpa.virginia.edu> Date: 16 Feb 90 23:20:10 GMT Sender: mmdf@uvaarpa.virginia.edu Reply-To: kayvan@mrspoc.transact.com Lines: 65 I've made a small modification to getopts.pl that allows me to do this: &Getopts("xyz") || die($USAGE); As it stood, Getopts would print out the "unknown option" messages but give no indication to the caller that invalid options had been entered by the user. The patch follows: *** getopts.pl~ Fri Feb 16 12:24:53 1990 --- getopts.pl Fri Feb 16 12:24:53 1990 *************** *** 4,7 ;# do Getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a ;# # side effect. sub Getopts { --- 4,9 ----- ;# do Getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a ;# # side effect. + ;# + ;# returns 1 if no errors, 0 otherwise. sub Getopts { *************** *** 8,11 local($argumentative) = @_; local(@args,$_,$first,$rest); @args = split( / */, $argumentative ); --- 10,14 ----- local($argumentative) = @_; local(@args,$_,$first,$rest); + local($retval) = 1; @args = split( / */, $argumentative ); *************** *** 33,36 else { print stderr "Unknown option: $first\n"; if($rest ne '') { $ARGV[0] = "-$rest"; --- 36,40 ----- else { print stderr "Unknown option: $first\n"; + $retval = 0; if($rest ne '') { $ARGV[0] = "-$rest"; *************** *** 41,44 } } } --- 45,49 ----- } } + $retval; }