home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / OPTIONS.ICN < prev    next >
Text File  |  1991-09-05  |  4KB  |  97 lines

  1. ############################################################################
  2. #
  3. #    Name:     options.icn
  4. #
  5. #    Title:     Get command-line options
  6. #
  7. #    Authors: Robert J. Alexander and Gregg M. Townsend
  8. #
  9. #    Date:     July 31, 1991
  10. #
  11. ############################################################################
  12. #  
  13. #     options(arg,optstring) -- Get command line options.
  14. #  
  15. #     This procedure analyzes the -options on the command line
  16. #  invoking an Icon program.  The inputs are:
  17. #  
  18. #       arg         the argument list as passed to the main procedure.
  19. #
  20. #       optstring   a string of allowable option letters. If a
  21. #                   letter is followed by ":" the corresponding
  22. #                   option is assumed to be followed by a string of
  23. #                   data, optionally separated from the letter by
  24. #                   space. If instead of ":" the letter is followed
  25. #                   by a "+", the parameter will converted to an
  26. #                   integer; if a ".", converted to a real.  If opt-
  27. #                   string is omitted any letter is assumed to be
  28. #                   valid and require no data.
  29. #  
  30. #     It returns a table containing the options that were specified.
  31. #  The keys are the specified option letters. The assigned values are
  32. #  the data words following the options, if any, or 1 if the option
  33. #  has no data. The table's default value is &null.
  34. #
  35. #     Upon return, the option arguments are removed from arg, leaving
  36. #  only the non-option arguments.
  37. #  
  38. #     If an error is detected, stop() is called with an appropriate
  39. #  error message.
  40. #
  41. #     Options may be freely interspersed with non-option arguments.
  42. #  An argument of "-" is treated as a non-option.  The special argument
  43. #  "--" terminates option processing.  Non-option arguments are returned
  44. #  in the original argument list for interpretation by the caller.
  45. #
  46. #     An argument of the form @filename (a "@" immediately followed
  47. #  by a file name) causes options() to replace that argument with
  48. #  arguments retrieved from the file "filename".  Each line of the file
  49. #  is taken as a separate argument, exactly as it appears in the file.
  50. #  Arguments beginning with - are processed as options, and those
  51. #  starting with @ are processed as nested argument files.  An argument
  52. #  of "--" causes all remaining arguments IN THAT FILE ONLY to be
  53. #  treated as non-options (including @filename arguments).
  54. #  
  55. ############################################################################
  56.  
  57. procedure options(arg,optstring)
  58.    local c,f,filearg,flist,fn,i,ignore,o,otab,p,x
  59.    /optstring := string(&letters)
  60.    otab := table()
  61.    flist := []
  62.    while x := get(arg) do
  63.       if /x then ignore := &null
  64.       else x ? {
  65.      if ="-" & not pos(0) & /ignore then {
  66.         if ="-" & pos(0) then ignore := 1
  67.         else while c := move(1) do
  68.            if i := find(c,optstring) + 1 then
  69.           otab[c] :=
  70.              if any(':+.',o := optstring[i]) then {
  71.             p := "" ~== tab(0) | get(arg) |
  72.                   stop("No parameter following -",c)
  73.             case o of {
  74.                ":": p
  75.                "+": integer(p) |
  76.                  stop("-",c," needs numeric parameter")
  77.                ".": real(p) |
  78.                  stop("-",c," needs numeric parameter")
  79.                }
  80.             }
  81.              else 1
  82.            else stop("Unrecognized option: -",c)
  83.         }
  84.      else if ="@" & not pos(0) & /ignore then {
  85.         f := open(fn := tab(0)) | stop("Can't open ",fn)
  86.         filearg := []
  87.         while put(filearg,read(f))
  88.         close(f)
  89.         push(arg)
  90.         while push(arg,pull(filearg))
  91.         }
  92.          else put(flist,x)
  93.       }
  94.    while push(arg,pull(flist))
  95.    return otab
  96. end
  97.