home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
OL.LZH
/
PROCS.LZH
/
OPTIONS.ICN
< prev
next >
Wrap
Text File
|
1991-09-05
|
4KB
|
97 lines
############################################################################
#
# Name: options.icn
#
# Title: Get command-line options
#
# Authors: Robert J. Alexander and Gregg M. Townsend
#
# Date: July 31, 1991
#
############################################################################
#
# options(arg,optstring) -- Get command line options.
#
# This procedure analyzes the -options on the command line
# invoking an Icon program. The inputs are:
#
# arg the argument list as passed to the main procedure.
#
# 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 table containing the options that were specified.
# The keys 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.
#
# Upon return, the option arguments are removed from arg, leaving
# only the non-option arguments.
#
# If an error is detected, stop() is called with an appropriate
# error message.
#
# Options may be freely interspersed with non-option arguments.
# An argument of "-" is treated as a non-option. The special argument
# "--" terminates option processing. Non-option arguments are returned
# in the original argument list for interpretation by the caller.
#
# An argument of the form @filename (a "@" immediately followed
# by a file name) causes options() to replace that argument with
# arguments retrieved from the file "filename". Each line of the file
# is taken as a separate argument, exactly as it appears in the file.
# Arguments beginning with - are processed as options, and those
# starting with @ are processed as nested argument files. An argument
# of "--" causes all remaining arguments IN THAT FILE ONLY to be
# treated as non-options (including @filename arguments).
#
############################################################################
procedure options(arg,optstring)
local c,f,filearg,flist,fn,i,ignore,o,otab,p,x
/optstring := string(&letters)
otab := table()
flist := []
while x := get(arg) do
if /x then ignore := &null
else x ? {
if ="-" & not pos(0) & /ignore then {
if ="-" & pos(0) then ignore := 1
else 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 -",c)
case o of {
":": p
"+": integer(p) |
stop("-",c," needs numeric parameter")
".": real(p) |
stop("-",c," needs numeric parameter")
}
}
else 1
else stop("Unrecognized option: -",c)
}
else if ="@" & not pos(0) & /ignore then {
f := open(fn := tab(0)) | stop("Can't open ",fn)
filearg := []
while put(filearg,read(f))
close(f)
push(arg)
while push(arg,pull(filearg))
}
else put(flist,x)
}
while push(arg,pull(flist))
return otab
end