home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-05-09 | 2.5 KB | 72 lines | [TEXT/PICN] |
- ############################################################################
- #
- # getopt.icn
- #
- # getopt(arg,optstring) -- Get options from parameter string.
- #
- # This procedure analyzes the parameter string. Its inputs are:
- #
- # arg the argument list as passed to the main pro-
- # cedure.
- #
- # optstring a string of allowable option letters. If a
- # letter is followed by ":" the corresponding
- # option is assumed to be followed by a string of
- # data, optionally separated from the letter by
- # space. If instead of ":" the letter is followed
- # by a "+", the parameter will converted to an
- # integer; if a ".", converted to a real. If opt-
- # string is omitted any letter is assumed to be
- # valid and require no data.
- #
- # It returns a list consisting of two items:
- #
- # [1] a table of options specified. The entry values are the
- # specified option letters. The assigned values are the
- # data words following the options, if any, or 1 if the
- # option has no data. The table's default value is &null.
- #
- # [2] a list of remaining parameters in the parameter string
- # (usually file names). A "-" which is not followed by a
- # letter is taken as a file name rather than an option.
- #
- # If an error is detected, stop() is called with an appropriate
- # error message. After calling getopt() the original argument list,
- # arg, is empty.
- #
- ############################################################################
- #
- # Many thanks to Bob Alexander, who wrote this procedure and placed it in
- # the public domain.
- #
- ############################################################################
-
- procedure getopt(arg,optstring)
- local x,i,c,otab,flist,o,p
- /optstring := string(&lcase ++ &ucase)
- otab := table()
- flist := []
- while x := get(arg) do
- x ? {
- if ="-" & not pos(0) then
- while c := move(1) do
- if i := find(c,optstring) + 1 then
- otab[c] :=
- if any(':+.',o := optstring[i]) then {
- p := "" ~== tab(0) | get(arg) |
- stop("No parameter following ",x)
- case o of {
- ":": p
- "+": integer(p) |
- stop("-",c," needs numeric parameter")
- ".": real(p) |
- stop("-",c," needs numeric parameter")
- }
- }
- else 1
- else stop("Unrecognized option: ",x)
- else put(flist,x)
- }
- return [otab,flist]
- end
-