home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume29 / parseargs / part02 / parseargs.pl < prev    next >
Encoding:
Text File  |  1992-05-19  |  2.7 KB  |  82 lines

  1. ;#########################################################################
  2. ;# ^FILE: parseargs.pl - parseargs for perl programs
  3. ;#
  4. ;# ^DESCRIPTION:
  5. ;#    This file defines a perl function named parseargs to parse
  6. ;#    command-line arguments for perl scripts.
  7. ;#
  8. ;# ^HISTORY:
  9. ;#    02/25/91    Brad Appleton    <brad@ssd.csd.harris.com>    Created
  10. ;##^^#####################################################################
  11.  
  12.  
  13. ;########
  14. ;# ^FUNCTION: parseargs - parse command-line argument vectors
  15. ;#
  16. ;# ^SYNOPSIS:
  17. ;#    rc = &parseargs( @argv, $argd )
  18. ;#
  19. ;# ^PARAMETERS:
  20. ;#    argv -- the vector of command-line arguments (usually ARGV).
  21. ;#    argd -- the argument-description string
  22. ;#
  23. ;# ^DESCRIPTION:
  24. ;#    Parseargs will invoke parseargs(1) to parse the command-line given
  25. ;#    in <argv> for the command defined by <argd>.  The resulting values
  26. ;#    will be assigned to the variables indicated by the argument-description
  27. ;#    string.
  28. ;#
  29. ;# ^REQUIREMENTS:
  30. ;#    Any desired initial values for variables from the argument-description
  31. ;#    string should be assigned BEFORE calling this function.
  32. ;#
  33. ;#    The following global variables from package "main" may be assigned
  34. ;#    before calling parseargs:
  35. ;#
  36. ;#       PARSEOPTS -- any extra options to pass to parseargs() (default="-u")
  37. ;#
  38. ;# ^SIDE-EFFECTS:
  39. ;#    The global variable PARSEARGS will contain the command-line used to
  40. ;#    invoke parseargs(1).
  41. ;#
  42. ;#    ARGV and (and any other variables named in <argd>) may be overwritten.
  43. ;#
  44. ;# ^RETURN-VALUE:
  45. ;#    The exit code returned by parseargs(1).
  46. ;#
  47. ;# ^ALGORITHM:
  48. ;#    - read $PARSECNTL environment variable and use corresponding syntax
  49. ;#      (long-options or short options) when invoking parseargs(1)
  50. ;#    - set defaults for PARSEOPTS
  51. ;#    - build the parseargs command (dont forget to quote arguments).
  52. ;#    - run parseargs(1) and evaluate the output unless $?
  53. ;##^^####
  54.  
  55. sub parseargs {
  56.    local($argd, @argv) = ( pop(@_), $0, @_ );
  57.    local($unset, $sh, $env, $end) = ( '-u', '-s', '-e', '--' );
  58.    local($parse_output, $_);
  59.  
  60.    $_ = $main'ENV{'PARSECNTL'};
  61.    if ( /[^!~\^]\s*[KkLl]/ ) {  ## KeyWords only!
  62.       ($unset, $sh, $env, $end) = ( '+unset', '+shell', '+env', '++' );
  63.    }
  64.  
  65.    if ( ! $main'PARSEOPTS )  { $main'PARSEOPTS = "$unset"; }
  66.  
  67.    grep( s/'/'\\''/g, @argv );  ## escape embedded quotes
  68.    $PARSEARGS = "parseargs $main'PARSEOPTS $sh perl $env ARGD $end ";
  69.    $PARSEARGS .= "'" . join( "' '", @argv ) . "'";
  70.  
  71.    $main'ENV{'ARGD'} = $argd;         ## put argd-array into environment
  72.    $parse_output = `$PARSEARGS`;      ## invoke parseargs(1)
  73.    eval $parse_output unless $?;      ## evaluate the output-script
  74.    if ( $? ) {
  75.       $! = 0;
  76.       die "\n";
  77.    }
  78.    return $?;
  79. }
  80.  
  81. 1;
  82.