An Enhanced FilterOptions Package
By Ted Ersek
      ersektr@navair.navy.mil

The standard package Utilities`FilterOptions`  was modified to include a new function called CheckOptions.  FilterOptions and CheckOptions are closely related, so it seemed logical make an addition to the FilterOptions package.  Before evaluating the examples below you should replace the FilterOptions.m  file in the \(TraditionalForm\`\(\(\(\\)\(AddOns\)\)\StandardPackages\)\Utilities\)  folder with the new version of the same file posted on MathSource along with this notebook.  Once that is done evaluate the next cell.

<< Utilities`FilterOptions`

The only changes in the new version of FilterOptions.m  are the addition of a CheckOptions function, and a definition for a new message named  General::opts.  The usage message of CheckOptions is shown below.

?CheckOptions

Examples

In the next cell CheckOptions posts a message indicating that MaxBend and PlotJoined aren't valid options for Plot3D and returns False.

CheckOptions[Plot3D, PlotPoints -> 40, 
      PlotRange -> {0, 20}, 
MaxBend  -> 7, 
      PlotJoined -> True]

In the next cell CheckOptions posts a message indicating that PlotJoined isn't a valid options for Plot3D and returns False.

CheckOptions[Plot3D, PlotPoints -> 40, 
      PlotRange -> {0, 20}, 
PlotJoined -> 
        True]

The next two example return True, and post no messages because the options provided are valid options for Plot3D.

CheckOptions[
      Plot3D, {PlotPoints -> 40, PlotRange -> {0, 20}}]

CheckOptions[Plot3D]

Applications

A simple function that uses CheckOptions is defined in the next cell.

ClearAll[MyFunction];
Options[MyFunction] = {opt1 -> val1,  opt2 -> val2,  
          opt3 -> val3};

MyFunction[x_, 
        opts___?OptionQ] := (
CheckOptions[MyFunction, 
          opts]; 
x Exp[x])

When the next two cells are evaluated no messages are posted.  In the first example no options are used.  In the second example the option used is recognized by MyFunction.

MyFunction[3]

MyFunction[t,  opt1 -> 20]

In the next two cells MyFunction is given one or more option that doesn't belong to MyFunction.  When these cells are evaluated a message is posted indicating that MyFunction doesn't know what to do with one or more of the options.

MyFunction[t, Trig -> True, opt1 -> 20]

MyFunction[t, Trig -> True, Modulus -> 23, 
      opt1 -> 20]

One can also use CheckOptions to ensure a function is undefined when given options it doesn't recognize.  In the next cell MyFunction is defined using this approach.

ClearAll[MyFunction];
Options[MyFunction] = {opt1 -> val1,  opt2 -> val2,  
          opt3 -> val3};

MyFunction[x_, opts___?OptionQ] /; 
        CheckOptions[MyFunction, opts] := 
x Exp[
          x]

This new definition of MyFunction is used in the cells below.

MyFunction[3]

MyFunction[t,  opt1 -> 20]

MyFunction[t, Trig -> True, opt1 -> 20]

MyFunction[t, Trig -> True, Modulus -> 23, 
      opt1 -> 20]