home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / EXAMPLES.PAK / OPTIONUT.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  2.9 KB  |  98 lines

  1.  
  2. BeginPackage["Examples`OptionUtilities`"]
  3.  
  4. OptionList::usage =
  5. "OptionList[expr] returns a list of all valid options found in expr."
  6.  
  7. OptionSelect::usage =
  8. "OptionSelect[input, target] replaces options found in target with
  9. any corresponding options found in input."
  10.  
  11. OptionExtract::usage =
  12. "OptionExtract[args, known] returns a list {parameters, options} unless
  13. unknown options are found, in which case the returned value is $Failed.
  14. args is scanned from the last element to the last non-option argument,
  15. and any replacement rules found are substituted for the corresponding
  16. rules in known.  Remaining args are returned as parameters."
  17.  
  18. Begin["`Private`"]
  19.  
  20. (* ==== OptionList ==================================================== *)
  21.  
  22. OptionList[expr_] := Flatten[Options[expr]]
  23.  
  24. (* ==== OptionSelect ================================================== *)
  25.  
  26. OptionSelect[input_, target_] :=
  27.     CompoundExpression[
  28.         targetlist = OptionList[target],
  29.         iOptionSelect[input],
  30.         targetlist
  31.     ]
  32.  
  33. iOptionSelect[opts_List] := Map[iOptionSelect[#] &, opts]
  34.  
  35. iOptionSelect[rule_] :=
  36.     Block[{name, n},
  37.         name = First[rule];
  38.         For[n = 1, n <= Length[targetlist], n++,
  39.             If[name === targetlist[[n, 1]],
  40.                 targetlist[[n]] = rule;
  41.                 Break[]
  42.             ]
  43.         ]
  44.     ] /; Head[rule] === Rule || Head[rule] === RuleDelayed
  45.  
  46. (* ==== OptionExtract ================================================= *)
  47.  
  48. OptionExtract[args_List, known_] :=
  49.     Block[{n},
  50.         validlist = OptionList[known];
  51.         n = Length[args];
  52.         While[n > 0,
  53.             If[OptionQ[args[[n]]],
  54.                 If[InvalidOption[args[[n]]], Return[$Failed]],
  55.             (* else, break at the last non-option. *)
  56.                 Break[]
  57.             ];
  58.             n--
  59.         ];
  60.         (* at this point, all options have been checked. *)
  61.         Append[Take[args, n], validlist]
  62.     ]
  63.  
  64. InvalidOption[opt_List] :=
  65.     Block[{i},
  66.         For[i = 1, i <= Length[opt], i++,
  67.             If[InvalidOption[opt[[i]]], Return[True]]
  68.         ];
  69.         False
  70.     ]
  71.  
  72. InvalidOption[opt_] :=
  73.     Block[{i, name},
  74.         name = First[opt];
  75.         For[i = 1, i <= Length[validlist], i++,
  76.             If[name === validlist[[i, 1]],
  77.                 validlist[[i]] = opt;
  78.                 Return[False]
  79.             ]
  80.         ];
  81.         Message[OptionExtract::unkopt, opt];
  82.         Return[True]
  83.     ] /; Head[opt] === Rule || Head[opt] === RuleDelayed
  84.  
  85. InvalidOption[opt_] := (Message[OptionExtract::nonrep, opt]; True)
  86.  
  87. OptionExtract::unkopt = "Option `1` not recognized as a valid option."
  88.  
  89. OptionExtract::nonrep = "Option specification `1` was expected to be
  90. a replacement rule or list of replacement rules."
  91.  
  92. (* ==== End =========================================================== *)
  93.  
  94. End[]
  95.  
  96. EndPackage[]
  97.  
  98.