I my opinion, this is exactly the way to go. As pointed out by Anon you can do the assignment directly in the variable list of Module
, but otherwise it is equivalent to the example shown in the Setting Up Functions with Optional Arguments tutorial:
odeplot[de_, y_, {x_, x0_, x1_}, opts : OptionsPattern[]] :=
Module[{sol},
sol = NDSolve[de, y, {x, x0, x1},
FilterRules[{opts}, Options[NDSolve]]];
If[Head[sol] === NDSolve,
$Failed,
Plot[Evaluate[y /. sol], {x, x0, x1},
Evaluate[FilterRules[{opts}, Options[Plot]]]]
]
]
A nice thing of FilterRules
is that it even works with nested option lists, which is often possible in Mathematica
Plot[2 x, {x, 0, 1}, {ColorFunction -> Hue, {ColorFunctionScaling -> False}}]
This will then even work with your example
foo[opts : OptionsPattern[]] := FilterRules[{opts}, Options[Plot]]
foo[{ColorFunction -> Hue, {ColorFunctionScaling -> False, {MaxIterations -> 30}}}]
goodOpts=FilterRules[{opts},Options[bar]]
, and the assignment can be made in the first argument of module. $\endgroup$opts : OptionsPattern[{foo, bar}]
assumingfoo
itself takes options. $\endgroup$