Modula-2 || Compiler & Tools || Library || Search Engine
PROCEDURE InitArgs(infostring: ARRAY OF CHAR); PROCEDURE Usage; PROCEDURE AllArgs; PROCEDURE GetFlag(VAR flag: CHAR): BOOLEAN; PROCEDURE GetOpt( VAR flag: CHAR; VAR plus: BOOLEAN): BOOLEAN; PROCEDURE UngetOpt; PROCEDURE GetArg(VAR argument: ARRAY OF CHAR): BOOLEAN; PROCEDURE UngetArg; PROCEDURE FetchString(VAR string: ARRAY OF CHAR); PROCEDURE FetchInt( VAR number: INTEGER); PROCEDURE FetchCard( VAR number: CARDINAL); PROCEDURE FetchOct( VAR number: CARDINAL); PROCEDURE FetchHex( VAR number: CARDINAL);
The procedures are used as follows:
InitArgs specifies infostring for Usage and (re)starts the reading cycle, i.e. makes the first command argument the next one to be read.
Usage prints 'Usage: command infostring' onto standard-error and aborts program execution ('command' stands here for the actual processes' name).
AllArgs calls Usage if any command arguments are not yet read.
GetFlag and GetOpt read one flag resp. option from the argument list or return FALSE if all of them have been read. GetOpt sets plus TRUE if the actual option is of the kind +x, otherwise FALSE.
GetArg reads one arbitrary argument or returns FALSE if all arguments have been read already.
FetchString, FetchInt, FetchCard, FetchOct and FetchHex read a value of the specified type. If the selected argument (part) is missing, is not of the required type or if a numerical value exceeds the range of number, Usage is called implicitly.
UngetOpt and UngetArg push back one flag/option resp. argument per call to the list of not yet read command arguments. Note that UngetOpt is not able to skip command arguments that have been read using GetArg or one of the FetchXXX procedures.
xflag := FALSE; number := 1; string := defaultstring; InitArgs("[-x] [-s string] [-nnn] [file]..."); WHILE GetFlag(flag) DO CASE flag OF "x": xflag := TRUE; | "s": FetchString(string); | "0".."9": UngetOpt; FetchCard(number); ELSE Usage END; END; (*WHILE GetFlag*) WHILE GetArg(filename) DO IF StrCmp(filename,"-") = 0 THEN (* process stdin *) ELSE (* process filename *) END; END; (*WHILE GetArg*)
Modula-2 || Compiler & Tools || Library || Search Engine