home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / sharew / exoten / icon / options.icn < prev    next >
Encoding:
Text File  |  1990-03-05  |  2.9 KB  |  76 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:     November 9, 1989
  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. #     If an error is detected, stop() is called with an appropriate
  36. #  error message.
  37. #
  38. #     Options may be freely interspersed with non-option arguments.
  39. #  An argument of "-" is treated as a non-option.  The special argument
  40. #  "--" terminates option processing.  Non-option arguments are returned
  41. #  in the original argument list for interpretation by the caller.
  42. #  
  43. ############################################################################
  44.  
  45. procedure options(arg,optstring)
  46.    local x,i,c,otab,flist,o,p
  47.    /optstring := string(&letters)
  48.    otab := table()
  49.    flist := []
  50.    while x := get(arg) do
  51.       x ? {
  52.          if ="-" & not pos(0) then {
  53.             if ="-" & pos(0) then break
  54.             while c := move(1) do
  55.                if i := find(c,optstring) + 1 then
  56.                   otab[c] :=
  57.                      if any(':+.',o := optstring[i]) then {
  58.                         p := "" ~== tab(0) | get(arg) |
  59.                               stop("No parameter following -",c)
  60.                         case o of {
  61.                            ":": p
  62.                            "+": integer(p) |
  63.                                  stop("-",c," needs numeric parameter")
  64.                            ".": real(p) |
  65.                                  stop("-",c," needs numeric parameter")
  66.                            }
  67.                         }
  68.                      else 1
  69.                else stop("Unrecognized option: -",c)
  70.          }
  71.          else put(flist,x)
  72.       }
  73.    while push(arg,pull(flist))
  74.    return otab
  75. end
  76.