Article View: gnu.misc.discuss
Article #74400replacement for getopt/getopt_long
From: Brad Appleton
Date: Wed, 01 May 1991 19:13
Date: Wed, 01 May 1991 19:13
153 lines
7647 bytes
7647 bytes
I have long wanted to replace getopt (and getopt_long) with something
where I just give it an array of structs and it parses my arguments
for me (actually assigns my variables instead of me having too loop
through some canonicalized command-line).
I was going to do this for GNU after I finished parseargs but it would
duplicate so much of the code in parseargs that it seems ridiculous.
The problems with parseargs is
a) it is not CopyLeft (although it is PD with no copyright)
b) it contains code to parse DOS and VMS style command-lines as
well as Unix style command-lines
c) it is somewhat large for a command-line parsing library
d) my own pet-peeve, due to backward compatibilty concerns, the
argument name and the argument keyword had to be stored in the
same field (instead of in separate fields).
Would it be feasible to provide a stripped-down version of parseargs
modified slightly and geered specifically for GNU? Since the original
code is PD with no copyright, I think the slightly-modified GNU version
could be placed under copyleft (as long as proper credit is given).
For those of you who are unfamiliar with parseargs, a brief description
follows - the rest of you can skip the rest of this article.
______________________ "And miles to go before I sleep." ______________________
Brad Appleton brad@ssd.csd.harris.com Harris Computer Systems
uunet!hcx1!brad Fort Lauderdale, FL USA
~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~
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>...]
______________________ "And miles to go before I sleep." ______________________
Brad Appleton brad@ssd.csd.harris.com Harris Computer Systems
uunet!hcx1!brad Fort Lauderdale, FL USA
~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~
Technical Details
Message-ID:
<3224@travis.csd.harris.com>Path:
arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!spool.mu.edu!sdd.hp.com!caen!uflorida!travis!bradShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1025b4,11166b25cec062a8,start X-Google-Attributes: gid1025b4,public X-Google-ArrivalTime: 1991-05-02 15:40:47 PST Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!spool.mu.edu!sdd.hp.com!caen!uflorida!travis!brad From: brad@SSD.CSD.HARRIS.COM (Brad Appleton) Newsgroups: gnu.misc.discuss Subject: replacement for getopt/getopt_long Summary: Could we use parseargs? Keywords: getopt, parseargs Message-ID: <3224@travis.csd.harris.com> Date: 1 May 91 19:13:10 GMT Sender: news@travis.csd.harris.com Organization: Harris Computers Systems Division, Fort Lauderdale,FL Lines: 153 Posted: Wed May 1 14:13:10 1991