home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume17 / parseargs / part00 next >
Encoding:
Internet Message Format  |  1991-03-18  |  6.1 KB

  1. From: brad@hcx1.ssd.csd.harris.com (Brad Appleton)
  2. Newsgroups: comp.sources.misc
  3. Subject: v17i045:  parseargs - functions to parse command line arguments, Part00/12
  4. Message-ID: <1991Mar17.195904.17451@sparky.IMD.Sterling.COM>
  5. Date: 17 Mar 91 19:59:04 GMT
  6. Approved: kent@sparky.imd.sterling.com
  7. X-Checksum-Snefru: 7c158c3f e273509b 9c4d42a9 f1705651
  8.  
  9. Submitted-by: Brad Appleton <brad@hcx1.ssd.csd.harris.com>
  10. Posting-number: Volume 17, Issue 45
  11. Archive-name: parseargs/part00
  12.  
  13.  
  14.                                   PARSEARGS
  15.  
  16.                         extracted from Eric Allman's
  17.  
  18.                             NIFTY UTILITY LIBRARY
  19.  
  20.                           Created by Eric P. Allman
  21.                              <eric@Berkeley.EDU>
  22.  
  23.                          Modified by Peter da Silva
  24.                             <peter@Ferranti.COM>
  25.  
  26.                    Modified and Rewritten by Brad Appleton
  27.                           <brad@SSD.CSD.Harris.COM>
  28.  
  29.  
  30.  Welcome to parseargs! Dont let the initial size of this package scare you.
  31.  over 75% of it is English text, and more than 50% of the source is comments.
  32.  
  33.  Parseargs is a set of functions to parse command-line arguments. Unlike
  34.  getopt and its variants, parseargs does more than just split up the
  35.  command-line into some canonical form. Parseargs will actually parse the
  36.  command-line, assigning the appropriate command-line values to the
  37.  corresponding variables, and will verify the command-line syntax (and print
  38.  a usage message if necessary). Furthermore, many features of it's parsing
  39.  behavior are configurable at run-time. Some of these features include the
  40.  following:
  41.  
  42.      o  Prompting the user for missing arguments
  43.      o  Allowing keywords (+count=4) and/or options (-c4)
  44.      o  Checking for default arguments in an environment variable
  45.      o  Ignoring bad syntax instead of terminating
  46.      o  Ignoring upper/lower case on the command-line
  47.      o  Controlling the location of non-positional parameters
  48.      o  Controlling the contents (syntax and verbosity) of usage messages
  49.      o  Having long usage messages piped through a paging program
  50.  
  51.  Parseargs also allows for options that take an optional argument, and
  52.  options that take a (possibly optional) list of one or more arguments.
  53.  In addition, parseargs may be configured at compile-time to parse
  54.  command-lines in accordance with the native command-syntax of any of the
  55.  following operating systems:
  56.  
  57.      o  Unix
  58.      o  VAX/VMS
  59.      o  OS/2
  60.      o  MS-DOS
  61.      o  AmigaDOS
  62.  
  63.  Parseargs consists of a set of C-functions to parse arguments from the
  64.  command-line, from files, from strings, from linked-lists, and from
  65.  string-vectors. Also included is a command-line interface which will parse
  66.  arguments for shell scripts (sh, csh/tcsh, ksh, bash, and rc), awk-scripts,
  67.  and perl-scripts.
  68.  
  69.  The basic structure used by parseargs is the argument-descriptor (sometimes
  70.  called "argdesc" for brevity).  An array/string of argdescs is declared by
  71.  the user to describe the command in question.  The resulting argdesc-array
  72.  is passed to all the parseargs functions and is used to hold all information
  73.  about the command. a sample argdesc-array is shown below.
  74.  
  75.     STARTOFARGS,
  76.     { 'a', ARGVALOPT, argStr,   &area,    "AREAcode : optional area-code" },
  77.     { 'g', ARGLIST,   argStr,   &groups,  "newsGROUPS : groups to test" },
  78.     { 'r', ARGOPT,    argInt,   &count,   "REPcount : repetition factor" },
  79.     { 's', ARGOPT,    argChar,  &sepch,   "SEPchar : field separator" },
  80.     { 'x', ARGOPT,    argBool,  &xflag,   "Xflag : turn on X-mode" },
  81.     { ' ', ARGREQ,    argStr,   &name,    "name : name to use" },
  82.     { ' ', ARGLIST,   argStr,   &args,    "args : any remaining arguments" },
  83.     ENDOFARGS
  84.  
  85.  Once the above array/string is declared it is a simple matter to invoke
  86.  parseargs from C as in the following example:
  87.  
  88.     status = parseargs( argdesc_array, argv );
  89.  
  90.  or from a shell script as in the following example:
  91.  
  92.     echo "$ARGDESC_STR" | parseargs -s sh -- "$0" "$@" >tmp$$
  93.     if [ $? = 0 ] ; then
  94.        . tmp$$
  95.     fi
  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.  has 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. ______________________ "And miles to go before I sleep." ______________________
  134.  Brad Appleton                         Harris Corp., Computer Systems Division
  135.    Software Engineer                   2101 West Cypress Creek Road,  M/S 161 
  136.      brad@ssd.csd.harris.com           Fort Lauderdale, FL  33309-1892  USA
  137.        ...!uunet!hcx1!brad                 Phone: (305) 973-5360
  138. ~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~
  139.  
  140. exit 0 # Just in case...
  141. -- 
  142. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  143. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  144. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  145. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  146.