Re: 4GL command line parsing functions
Posted in 1995
>From: brian@anasazi.com (Brian Lee) >Date: Wed, 5 Jul 1995 21:20:12 GMT >X-Informix-List-Id: <news.15169> > >Does anyone know if 4GL has any built-in command line option parsing >functions? It doesn't have any facilities other than NUM_ARGS, which indicates how many arguments there are, and ARG_VAL, which returns the Nth argument. >I'm looking for something like the getopts in the bourne shell. Try this shell archive... Documentation, including sample use, included with code. Yours, Jonathan Leffler (johnl@informix.com) #include <disclaimer.h> : "@(#)shar.sh 1.9" #! /bin/sh # # This is a shell archive. # Remove everything above this line and run sh on the resulting file. # If this archive is complete, you will see this message at the end: # "All files extracted" # # Created: Wed Jul 5 16:20:30 PDT 1995 by johnl at Informix Software Ltd. # Files archived in this archive: # getopt.4gl # #-------------------- if [ -f getopt.4gl -a "$1" != "-c" ] then echo shar: getopt.4gl already exists else echo 'x - getopt.4gl (3222 characters)' sed -e 's/^X//' >getopt.4gl <<'SHAR-EOF' X{ X @(#)getopt.4gl 1.4 90/12/17 X @(#)Sphinx Informix Tools: General Library X @(#)Simulate GETOPT(3) X @(#)Author: JL X} X X{ XSimulate GETOPT(3) in I4GL X XOperation: XWhen called with an argument such as "abcf:t:x", the function scans Xthe command line argument list for the flags -a, -b -c or -x, or options -f and X-t. The flags are boolean values; the options have a text string associated Xwith them. This can be attached to the option argument (e.g. -ffilename) or in Xthe next argument (-f filename). X XDifferences from GETOPT(3): X1. Function returns optval (CHAR(1)) containing the flag letter and optarg X (CHAR(80)) containing option string. GETOPT(3) just returns the flag letter X and the global variable optarg points to the option string. X3. The optval returned for no more arguments is NULL. X3. The optval returned for a non-option (filename) argument is "-". X} X XDEFINE X optind INTEGER, { Where we've got through list } X argnum INTEGER, { Corresponds to ARGC } X argstr CHAR(80), { Current argument } X arglen INTEGER, { Length of current argument } X chridx INTEGER, { How far through argstr we have analysed } X no_opt INTEGER { Has `--' argument been read? } X X{ XTest program which also indicates how GETOPT(3) is used. X XMAIN X X DEFINE X optval CHAR(1), X optarg CHAR(80) X X CALL getopt("abcf:t:x") RETURNING optval, optarg X WHILE optval IS NOT NULL X DISPLAY "optval = ", optval, " optarg = ", optarg CLIPPED X CALL getopt("abcf:t:x") RETURNING optval, optarg X END WHILE X XEND MAIN X} X XFUNCTION getopt(optstr) X X DEFINE X optstr CHAR(20), { List of options as for GETOPT(3) } X optarg CHAR(80), { Returned option value } X optval CHAR(1), { Returned option flag } X sccs CHAR(1), X idx INTEGER, X i INTEGER X X { Initialisation } X IF optind <= 0 THEN X LET optind = 1 X LET argnum = NUM_ARGS() X LET chridx = 1 X LET arglen = 0 X LET no_opt = FALSE X LET sccs = "@(#)getopt.4gl 1.4 90/12/17" X END IF X X LET optarg = NULL X LET optval = NULL X X WHILE TRUE X X { Step to next argument } X IF chridx > arglen THEN X IF optind > argnum THEN X EXIT WHILE X END IF X LET argstr = ARG_VAL(optind) X LET arglen = LENGTH(argstr) X LET optind = optind + 1 X LET chridx = 2 X END IF X X IF argstr[1] != "-" OR no_opt = TRUE OR arglen = 1 THEN X { Non-option argument } X LET optval = "-" X LET optarg = argstr X LET arglen = 0 X EXIT WHILE X END IF X X IF argstr[2] == "-" THEN X { '--' argument. All following arguments are file name arguments } X LET no_opt = TRUE X LET arglen = 0 X CONTINUE WHILE X END IF X X LET optval = argstr[chridx] X LET chridx = chridx + 1 X LET idx = instring(optstr, optval) X IF idx = 0 THEN X { Unknown option } X LET optval = "?" X EXIT WHILE X END IF X X IF optstr[idx+1] != ":" THEN X { Flag-only option } X EXIT WHILE X END IF X X IF arglen <= chridx THEN X { Need to use next argument for value } X LET optarg = ARG_VAL(optind) X LET optind = optind + 1 X LET arglen = 0 { Use next argument next time round } X EXIT WHILE X END IF X X { Value of option attached to current argument } X LET optarg = argstr[chridx,arglen] X LET arglen = 0 { Use next argument next time round } X EXIT WHILE X X END WHILE X X RETURN optval, optarg X XEND FUNCTION {getopt} SHAR-EOF chmod 444 getopt.4gl if [ `wc -c <getopt.4gl` -ne 3222 ] then echo shar: getopt.4gl unpacked with wrong size fi # end of overwriting check fi echo All files extracted exit 0