(* :Title: FilterOptions *) (* :Author: Roman E. Maeder; Ted Ersek *) (* :Summary: This package provides a function for selecting valid options when passing options from one function to another. *) (* :Context: Utilities`FilterOptions` *) (* :Copyright: Copyright 1991-1999, Wolfram Research, Inc.*) (* :Source: Roman E. Maeder: Programming in Mathematica, Third Edition, Addison-Wesley, 1996. *) (* :Package Version: 2.0 *) (* :Mathematica Version: 3.0 *) BeginPackage["Utilities`FilterOptions`"] FilterOptions::usage = "FilterOptions[symbol, options..] returns a sequence of those options that are valid options for symbol. FilterOptions[{opts..}, options..] filters out options with names opts." CheckOptions::usage="CheckOptions[func,opts] returns True if opts \ is an empty list or includes only options supported by func. If opts \ includes options not supported by func, then an appropriate message \ is posted and False is returned. CheckOptions[func] returns True. \ The second argument in CheckOptions can be a list or a sequence of \ options."; Begin["`Private`"] FilterOptions[ command_Symbol, options___ ] := FilterOptions[ First /@ Options[command], options ] FilterOptions[ opts_List, options___ ] := Sequence @@ Select[ Flatten[{options}], MemberQ[opts, First[#]]& ] General::opts="Unknown options `1` in `2`"; CheckOptions[func_Symbol,opts___?OptionQ]:= Module[{lst,bad,mesg}, bad=List/@Complement[First/@Flatten[{opts}],First/@Options[func]]; lst={Part[#,1],#}& /@Flatten[{opts}]; bad=Last/@Intersection[lst,bad, SameTest->(First[#1]===First[#2]&)]; Which[ Length[bad]>1, (mesg=ToExpression[StringJoin[ToString[func],"::opts"]]; Message[Evaluate[mesg],bad,func]; False ), Length[bad]===1, (mesg=ToExpression[StringJoin[ToString[func],"::optx"]]; Message[Evaluate[mesg],First[bad],func]; False ), True,True ] ] End[ ] Protect[ FilterOptions, CheckOptions ] EndPackage[ ]