[comp.lang.perl] An improvement to getopts.pl

kayvan@mrspoc.transact.com (Kayvan Sylvan) (02/17/90)

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;
  }