[comp.archives] [gnu.utils.bug] Replacement for getopt/getopt_long

brad@HCX2.SSD.CSD.HARRIS.COM (Brad Appleton) (05/09/91)

Archive-name: unix/getopt/parseargs/1991-05-06
Archive-directory: ftp.uu.net:/comp.sources.misc/volume17/parseargs/ [192.48.96.2]
Original-posting-by: brad@HCX2.SSD.CSD.HARRIS.COM (Brad Appleton)
Original-subject: Replacement for getopt/getopt_long
Reposted-by: emv@msen.com (Edward Vielmetti, MSEN)


I have just recently released Parseargs on comp.sources.misc, I think it should
become a replacement for getopt/getopt_long since it provides (among other
things) their combined functionality. The main problem with parseargs is that
it is not copyleft. It is not copyrighted either however so as far as I know
there is nothing to stop a slightly modified version of it from being placed
under copyleft. The other problem with parseargs IMHO is that it is rather
large for a command-line parser. This may be addressed by removing much of
the code to support other operating systems and by stripping down some of
the more elaborate (but less used) functionality.

Anyway - here is a brief introduction to parseargs, let me know if you want
more info or would like to have it e-mailed to you (sorry - no FTP sites
yet available that I know of).
------------------------------------------------------------------------------- 

                                  PARSEARGS

                        extracted from Eric Allman's

                            NIFTY UTILITY LIBRARY

                          Created by Eric P. Allman
                             <eric@Berkeley.EDU>

                         Modified by Peter da Silva
                            <peter@Ferranti.COM>

                   Modified and Rewritten by Brad Appleton
                          <brad@SSD.CSD.Harris.COM>


 Welcome to parseargs! Dont let the initial size of this package scare you.
 over 75% of it is English text, and more than 50% of the source is comments.

 Parseargs is a set of functions to parse command-line arguments. Unlike
 getopt and its variants, parseargs does more than just split up the
 command-line into some canonical form. Parseargs will actually parse the
 command-line, assigning the appropriate command-line values to the
 corresponding variables, and will verify the command-line syntax (and print
 a usage message if necessary). Furthermore, many features of it's parsing
 behavior are configurable at run-time. Some of these features include the
 following:

     o  Prompting the user for missing arguments
     o  Allowing keywords (+count=4) and/or options (-c4)
     o  Checking for default arguments in an environment variable
     o  Ignoring bad syntax instead of terminating
     o  Ignoring upper/lower case on the command-line
     o  Controlling the location of non-positional parameters
     o  Controlling the contents (syntax and verbosity) of usage messages
     o  Having long usage messages piped through a paging program

 Parseargs also allows for options that take an optional argument, and
 options that take a (possibly optional) list of one or more arguments.
 In addition, parseargs may be configured at compile-time to parse
 command-lines in accordance with the native command-syntax of any of the
 following operating systems:

     o  Unix
     o  VAX/VMS
     o  OS/2
     o  MS-DOS
     o  AmigaDOS

 Parseargs consists of a set of C-functions to parse arguments from the
 command-line, from files, from strings, from linked-lists, and from
 string-vectors. Also included is a command-line interface which will parse
 arguments for shell scripts (sh, csh/tcsh/itcsh, ksh, bash, zsh, and rc),
 awk-scripts, and perl-scripts.

 The basic structure used by parseargs is the argument-descriptor (sometimes
 called "argdesc" for brevity).  An array/string of argdescs is declared by
 the user to describe the command in question.  The resulting argdesc-array
 is passed to all the parseargs functions and is used to hold all information
 about the command. a sample argdesc-array is shown below.

    STARTOFARGS,
    { 'a', ARGVALOPT, argStr,   &area,    "AREAcode : optional area-code" },
    { 'g', ARGLIST,   argStr,   &groups,  "newsGROUPS : groups to test" },
    { 'r', ARGOPT,    argInt,   &count,   "REPcount : repetition factor" },
    { 's', ARGOPT,    argChar,  &sepch,   "SEPchar : field separator" },
    { 'x', ARGOPT,    argBool,  &xflag,   "Xflag : turn on X-mode" },
    { ' ', ARGREQ,    argStr,   &name,    "name : name to use" },
    { ' ', ARGLIST,   argStr,   &args,    "args : any remaining arguments" },
    ENDOFARGS

 Once the above array/string is declared it is a simple matter to invoke
 parseargs from C as in the following example:

    status = parseargs( argdesc_array, argv );

 or from a shell script as in the following example:

    echo "$ARGDESC_STR" | parseargs -s sh -- "$0" "$@" >tmp$$
    test  $? = 0  &&  . tmp$$
    /bin/rm -f tmp$$

 And before you know it, your command-line had been parsed, all variables 
 have been assigned their corresponding values from the command-line, syntax
 has been verified, and a usage message (if required) has been printed. 

 Under UNIX, the command-line syntax (using single character options) for the
 above command would be:

    cmdname [-a [<areacode>]] [-g <newsgroups>...] [-r <repcount>]
            [-s <sepchar>] [-x]  <name>  [<args>...]

 The UNIX command-line syntax using keywords (or long options) would be:

    cmdname [+area [<areacode>]] [+groups <newsgroups>...] [+rep <repcount>]
            [+sep <sepchar>] [+x]  <name>  [<args>...]

 The VMS command-line syntax would be the following:

    cmdname [/AREA[=<areacode>]] [/GROUPS=<newsgroups>[,<newsgroups>...]
            [/REP=<repcount>] [/SEP=<sepchar>] [/X]  <name>
            [<args>[,<args>...]]

 The MS-DOS and OS/2 command-line syntax would be the following (unless the
 environment variable $SWITCHAR is '-' in which case UNIX syntax is used):

    cmdname [/a[=<areacode>]] [/g=<newsgroups>...] [/r=<repcount>]
            [/s=<sepchar>] [/x]  <name>  [<args>...]

  The AmigaDOS command-line syntax would be the following:

    cmdname [AREA [<areacode>]] [GROUPS <newsgroups>...] [REP <repcount>]
            [SEP <sepchar>] [X]  <name>  [<args>...]


 Please look at the README files and manpages for more detailed information!

______________________ "And miles to go before I sleep." ______________________
 Brad Appleton                         Harris Corp., Computer Systems Division
   Software Engineer                   2101 West Cypress Creek Road,  M/S 161 
     brad@ssd.csd.harris.com           Fort Lauderdale, FL  33309-1892  USA
       ...!uunet!hcx1!brad                 Phone: (305) 973-5190
~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~

-- comp.archives file verification
ftp.uu.net
total 360
-rw-r--r--  1 78          17611 Mar 30 22:46 patch03.Z
-rw-r--r--  1 78          17599 Mar 27 16:05 patch02.Z
-rw-r--r--  1 78          27388 Mar 18 22:44 part07.Z
-rw-r--r--  1 78          25471 Mar 18 22:44 part09.Z
-rw-r--r--  1 78          24778 Mar 18 22:43 part12.Z
-rw-r--r--  1 78          26522 Mar 18 22:43 part11.Z
-rw-r--r--  1 78          25983 Mar 18 22:43 part10.Z
-rw-r--r--  1 78          27414 Mar 18 22:43 part08.Z
-rw-r--r--  1 78           7956 Mar 18 22:43 patch01.Z
-rw-r--r--  1 78          25667 Mar 18 10:40 part05.Z
-rw-r--r--  1 78          25651 Mar 18 10:40 part03.Z
-rw-r--r--  1 78          25422 Mar 18 10:40 part04.Z
-rw-r--r--  1 78          25565 Mar 18 10:40 part06.Z
-rw-r--r--  1 78          26232 Mar 18 10:39 part02.Z
-rw-r--r--  1 78          28029 Mar 18 10:39 part01.Z
-rw-r--r--  1 78           3622 Mar 18 10:39 part00.Z
found parseargs ok
ftp.uu.net:/comp.sources.misc/volume17/parseargs/