ProgramArgs

This module provides access to the command line arguments passed to the program's invocation. They are mapped onto a standard channel, with each argument transformed to a single line of text. Newline characters embedded in arguments are converted to spaces. Interpreting the arguments is usually done by applying TextRider.Reader or TextRider.Scanner to the argument channel.

The number of arguments can be determined by calling args.ArgNumber. If the invokation was something like `foo bar 42', where `foo' is the command's name, then the channel's contents would look like this:

foo
bar
42

For the given example args.ArgNumber would return `2'.

Import List

    Channel
    Time
 
Class List
Channel
ErrorContext
Reader
Class Summary: Channel [Detail]
  +---Channel.Channel
       |
       +--ProgramArgs.Channel
Inherited Fields

From Channel.Channel:

          open, readable, res, writable

Method Summary
ArgNumber(): LONGINT

          Returns the number of command line arguments (excluding the program name itself) passed to the program.
Close()

          Flushes all buffers associated with ch, closes the channel, and frees all system resources allocated to it.
Flush()

          Flushes all buffers related to this channel.
GetModTime(VAR TimeStamp)

          Since the argument channel has no modification time, this procedure will always signal a `noModTime' error.
Length(): LONGINT

          Result is the number of bytes of data that this channel refers to.
NewReader(): Reader

          Attaches a new reader to the channel ch.
Inherited Methods

From Channel.Channel:

          ClearError, Close, Flush, GetModTime, Length, NewReader, NewWriter

 
Class Summary: ErrorContext [Detail]
  +---Msg.Context
       |
       +---Channel.ErrorContext
            |
            +--ProgramArgs.ErrorContext
Inherited Fields

From Msg.Context:

          id

Inherited Methods

From Channel.ErrorContext:

          GetTemplate

 
Class Summary: Reader [Detail]
  +---Channel.Reader
       |
       +--ProgramArgs.Reader
Inherited Fields

From Channel.Reader:

          base, bytesRead, positionable, res

Method Summary
Available(): LONGINT

          Returns the number of bytes available for the next reading operation.
Pos(): LONGINT

          Returns the current reading position associated with the reader r in channel r.base, i.e.
ReadByte(VAR BYTE)

          Reads a single byte from the channel r.base at the reading position associated with r and places it in x.
ReadBytes(VAR ARRAY OF BYTE, LONGINT, LONGINT)

          Reads n bytes from the channel r.base at the reading position associated with r and places them in x, starting at index start.
SetPos(LONGINT)

          Sets the reading position to newPos.
Inherited Methods

From Channel.Reader:

          Available, ClearError, Pos, ReadByte, ReadBytes, SetPos

 
Variable Summary
args-: Channel

          
Constant Summary
channelClosed

          
done

          
noModTime

          
noWriteAccess

          
outOfRange

          
readAfterEnd

          

Class Detail: Channel
Method Detail

ArgNumber

PROCEDURE (ch: Channel) ArgNumber(): LONGINT

Returns the number of command line arguments (excluding the program name itself) passed to the program.


Close

PROCEDURE (ch: Channel) Close()

Flushes all buffers associated with ch, closes the channel, and frees all system resources allocated to it. This invalidates all riders attached to ch, and they can't be used further. On success, i.e. if all read and write operations (including flush) completed successfully, ch.res is set to done. An opened channel can only be closed once, successive calls of Channel.Close are undefined. Note that unlike the Oberon System all opened channels have to be closed explicitly. Otherwise resources allocated to them will remain blocked.

[Description inherited from Close]

Redefines: Close


Flush

PROCEDURE (ch: Channel) Flush()

Flushes all buffers related to this channel. Any pending write operations are passed to the underlying OS and all buffers are marked as invalid. The next read operation will get its data directly from the channel instead of the buffer. If a writing error occurs during flushing, the field ch.res will be changed to writeError, otherwise it's assigned done. Note that you have to check the channel's Channel.res flag after an explicit flush yourself, since none of the attached writers will notice any write error in this case.

[Description inherited from Flush]

Redefines: Flush


GetModTime

PROCEDURE (ch: Channel) GetModTime(VAR mtime: TimeStamp)

Since the argument channel has no modification time, this procedure will always signal a `noModTime' error.

Redefines: GetModTime


Length

PROCEDURE (ch: Channel) Length(): LONGINT

Result is the number of bytes of data that this channel refers to. If ch represents a file, then this value is the file's size. If ch has no fixed length (e.g. because it's interactive), the result is noLength.

[Description inherited from Length]

Redefines: Length


NewReader

PROCEDURE (ch: Channel) NewReader(): Reader

Attaches a new reader to the channel ch. It is placed at the very start of the channel, and its Reader.res field is initialized to done. ch.res is set to done on success and the new reader is returned. Otherwise result is NIL and ch.res is changed to indicate the error cause. Note that always the same reader is returned if the channel does not support multiple reading positions.

[Description inherited from NewReader]

Redefines: NewReader

 
Class Detail: ErrorContext
 
Class Detail: Reader
Method Detail

Available

PROCEDURE (r: Reader) Available(): LONGINT

Returns the number of bytes available for the next reading operation. For a file this is the length of the channel r.base minus the current reading position, for an sequential channel (or a channel designed to handle slow transfer rates) this is the number of bytes that can be accessed without additional waiting. The result is `-1' if Channel.Close was called for the channel, or no more byte are available and the remote end of the channel has been closed. Note that the number of bytes returned is always a lower approximation of the number that could be read at once; for some channels or systems it might be as low as `1' even if tons of bytes are waiting to be processed.

[Description inherited from Available]

Redefines: Available


Pos

PROCEDURE (r: Reader) Pos(): LONGINT

Returns the current reading position associated with the reader r in channel r.base, i.e. the index of the first byte that is read by the next call to Reader.ReadByte resp. Reader.ReadBytes. This procedure will return noPosition if the reader has no concept of a reading position (e.g. if it corresponds to input from keyboard), otherwise the result is not negative.

[Description inherited from Pos]

Redefines: Pos


ReadByte

PROCEDURE (r: Reader) ReadByte(VAR x: BYTE)

Reads a single byte from the channel r.base at the reading position associated with r and places it in x. The reading position is moved forward by one byte on success, otherwise r.res is changed to indicate the error cause. Calling this procedure with the reader r placed at the end (or beyond the end) of the channel will set r.res to readAfterEnd. r.bytesRead will be `1' on success and `0' on failure. Calls to this procedure while `r.res # done' will be ignored.

[Description inherited from ReadByte]

Redefines: ReadByte


ReadBytes

PROCEDURE (r: Reader) ReadBytes(VAR x: ARRAY OF BYTE; 
                    start: LONGINT; 
                    n: LONGINT)

Reads n bytes from the channel r.base at the reading position associated with r and places them in x, starting at index start. The reading position is moved forward by n bytes on success, otherwise r.res is changed to indicate the error cause. Calling this procedure with the reader r placed less than n bytes before the end of the channel will will set r.res to readAfterEnd. r.bytesRead will hold the number of bytes that were actually read (being equal to n on success). Calls to this procedure while `r.res # done' will be ignored.

Pre-condition: `(n >= 0) & (0 <= start) & (start+n <= LEN (x))'

[Description inherited from ReadBytes]

Redefines: ReadBytes


SetPos

PROCEDURE (r: Reader) SetPos(newPos: LONGINT)

Sets the reading position to newPos. A negative value of newPos or calling this procedure for a reader that doesn't allow positioning will set r.res to outOfRange. A value larger than the channel's length is legal, but the following read operation will most likely fail with an readAfterEnd error unless the channel has grown beyond this position in the meantime. Calls to this procedure while `r.res # done' will be ignored, in particular a call with `r.res.code = readAfterEnd' error will not reset Reader.res to done.

[Description inherited from SetPos]

Redefines: SetPos

 
Variable Detail

args

VAR args-: Channel
Constant Detail

channelClosed

CONST channelClosed 

done

CONST done 

noModTime

CONST noModTime 

noWriteAccess

CONST noWriteAccess 

outOfRange

CONST outOfRange 

readAfterEnd

CONST readAfterEnd