home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume29 / parsearg / part00 < prev    next >
Encoding:
Text File  |  1992-05-18  |  5.5 KB  |  134 lines

  1. Newsgroups: comp.sources.misc
  2. From: brad@hcx1.ssd.csd.harris.com (Brad Appleton)
  3. Subject:  v29i115:  parseargs - functions to parse command line arguments, Part00/10
  4. Message-ID: <csm-v29i115=parseargs.131900@sparky.IMD.Sterling.COM>
  5. X-Md4-Signature: 48e50da5ceaaa5a2da066c61c92189c4
  6. Date: Sun, 17 May 1992 18:21:02 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: brad@hcx1.ssd.csd.harris.com (Brad Appleton)
  10. Posting-number: Volume 29, Issue 115
  11. Archive-name: parseargs/part00
  12. Environment: UNIX, VMS, MS-DOS, OS/2, Amiga
  13. Supersedes: parseargs: Volume 17, Issue 46-57
  14.  
  15.  
  16.                                   PARSEARGS
  17.  
  18.                         extracted from Eric Allman's
  19.  
  20.                             NIFTY UTILITY LIBRARY
  21.  
  22.                           Created by Eric P. Allman
  23.                              <eric@Berkeley.EDU>
  24.  
  25.                          Modified by Peter da Silva
  26.                             <peter@Ferranti.COM>
  27.  
  28.                    Modified and Rewritten by Brad Appleton
  29.                           <brad@SSD.CSD.Harris.COM>
  30.  
  31.  
  32.  Welcome to parseargs! Dont let the initial size of this package scare you.
  33.  over 75% of it is English text, and more than 50% of the source is comments.
  34.  
  35.  Parseargs is a set of functions to parse command-line arguments. Unlike
  36.  getopt and its variants, parseargs does more than just split up the
  37.  command-line into some canonical form. Parseargs will actually parse the
  38.  command-line, assigning the appropriate command-line values to the
  39.  corresponding variables, and will verify the command-line syntax (and print
  40.  a usage message if necessary). Furthermore, many features of it's parsing
  41.  behavior are configurable at run-time. Some of these features include the
  42.  following:
  43.  
  44.      o  Prompting the user for missing arguments
  45.      o  Allowing keywords (+count=4) and/or options (-c4)
  46.      o  Checking for default arguments in an environment variable
  47.      o  Ignoring bad syntax instead of terminating
  48.      o  Ignoring upper/lower case on the command-line
  49.      o  Controlling the location of non-positional parameters
  50.      o  Controlling the contents (syntax and verbosity) of usage messages
  51.      o  Having long usage messages piped through a paging program
  52.  
  53.  Parseargs also allows for options that take an optional argument, and
  54.  options that take a (possibly optional) list of one or more arguments.
  55.  In addition, parseargs may be configured at compile-time to parse
  56.  command-lines in accordance with the native command-syntax of any of the
  57.  following operating systems:
  58.  
  59.      o  Unix
  60.      o  VAX/VMS
  61.      o  OS/2
  62.      o  MS-DOS
  63.      o  AmigaDOS
  64.  
  65.  Parseargs consists of a set of C-functions to parse arguments from the
  66.  command-line, from files, from strings, from linked-lists, and from
  67.  string-vectors. Also included is a command-line interface which will parse
  68.  arguments for shell scripts (sh, csh/tcsh/itcsh, ksh, bash, zsh, and rc),
  69.  awk-scripts, perl-scripts and tcl-scripts.
  70.  
  71.  The basic structure used by parseargs is the argument-descriptor (sometimes
  72.  called "argdesc" for brevity).  An array/string of argdescs is declared by
  73.  the user to describe the command in question.  The resulting argdesc-array
  74.  is passed to all the parseargs functions and is used to hold all information
  75.  about the command. a sample argdesc-array is shown below.
  76.  
  77.     STARTOFARGS,
  78.     { 'a', ARGVALOPT, argStr,   &area,    "AREAcode : optional area-code" },
  79.     { 'g', ARGLIST,   argStr,   &groups,  "newsGROUPS : groups to test" },
  80.     { 'r', ARGOPT,    argInt,   &count,   "REPcount : repetition factor" },
  81.     { 's', ARGOPT,    argChar,  &sepch,   "SEPchar : field separator" },
  82.     { 'x', ARGOPT,    argBool,  &xflag,   "Xflag : turn on X-mode" },
  83.     { ' ', ARGREQ,    argStr,   &name,    "name : name to use" },
  84.     { ' ', ARGLIST,   argStr,   &args,    "args : any remaining arguments" },
  85.     ENDOFARGS
  86.  
  87.  Once the above array/string is declared it is a simple matter to invoke
  88.  parseargs from C as in the following example:
  89.  
  90.     status = parseargs( argv, argdesc_array );
  91.  
  92.  or from a shell script as in the following example:
  93.  
  94.     echo "$ARGDESC_STR" | parseargs -s sh -- "$0" "$@" >tmp$$
  95.     test  $? = 0  &&  . tmp$$
  96.     /bin/rm -f tmp$$
  97.  
  98.  And before you know it, your command-line had been parsed, all variables 
  99.  have been assigned their corresponding values from the command-line, syntax
  100.  have been verified, and a usage message (if required) has been printed. 
  101.  
  102.  Under UNIX, the command-line syntax (using single character options) for the
  103.  above command would be:
  104.  
  105.     cmdname [-a [<areacode>]] [-g <newsgroups>...] [-r <repcount>]
  106.             [-s <sepchar>] [-x]  <name>  [<args>...]
  107.  
  108.  The UNIX command-line syntax using keywords (or long options) would be:
  109.  
  110.     cmdname [+area [<areacode>]] [+groups <newsgroups>...] [+rep <repcount>]
  111.             [+sep <sepchar>] [+x]  <name>  [<args>...]
  112.  
  113.  The VMS command-line syntax would be the following:
  114.  
  115.     cmdname [/AREA[=<areacode>]] [/GROUPS=<newsgroups>[,<newsgroups>...]]
  116.             [/REP=<repcount>] [/SEP=<sepchar>] [/X]  <name>
  117.             [<args>[,<args>...]]
  118.  
  119.  The MS-DOS and OS/2 command-line syntax would be the following (unless the
  120.  environment variable $SWITCHAR is '-' in which case UNIX syntax is used):
  121.  
  122.     cmdname [/a[=<areacode>]] [/g=<newsgroups>...] [/r=<repcount>]
  123.             [/s=<sepchar>] [/x]  <name>  [<args>...]
  124.  
  125.  The AmigaDOS command-line syntax would be the following:
  126.  
  127.     cmdname [AREA [<areacode>]] [GROUPS <newsgroups>...] [REP <repcount>]
  128.             [SEP <sepchar>] [X]  <name>  [<args>...]
  129.  
  130.  
  131.  Please look at the README files and manpages for more detailed information!
  132.  
  133. exit 0 # Just in case...
  134.