Modula-2 || Compiler & Tools || Library || Search Engine


Ulm's Modula-2 Library:
Arguments


NAME

Arguments - procedures for command line processing

SYNOPSIS

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);

DESCRIPTION

The Arguments module supports reading and interpreting command arguments, according to the following standard:
-x
Flags are single characters preceded by a -.

+x
Options, however, can be preceded either by - or +.

-xy
Flags or options with the same prefix may be concatenated to one command argument, without repeating the prefix.

-x value
A value follows a flag/option as the rest of the command argument or as the next command argument. Nothing can follow a value in the same command argument. The type of a value may be one of the following: string, or an INTEGER, CARDINAL, octal or hexadecimal number.

-
as a command argument is interpreted as a non-flag/non-option argument. It should designate standard input or standard output in place of a file.

--
as a command argument terminates flag/option processing but is itself not interpreted as an argument. Successing command arguments, even when beginning with - or + are considered not to contain flags nor options.

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.

EXAMPLE

The following example may illustrate how to use some of the 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*)

SEE ALSO

ARGC, StrToNum, Strings, StrSpec, StdIO

AUTHOR

Martin Hasch, University of Ulm
Edited by: borchert, last change: 1999/01/25, revision: 1.3, converted to HTML: 1999/01/25

Modula-2 || Compiler & Tools || Library || Search Engine