Both the previous examples are perfect if you know explicitly what option you want to pass.
In the more general case you may want to give none, one, or several of a compound set of options to more than one function chosen from a predefined set of functions.
You may even want to give a function as an argument , or option, to a function which can then utilise options given in the primary function call.
Hopefully the example below shows how to do this for built in and user defined functions, it makes use of OptionValue to access option values and FilterRules to ensure that only valid options are passed to any function.
Clear[f, ff, fff];
(* ff, a function to do something, with a parameter *)
Options[ff] = {Squared -> False};
ff[x_, OptionsPattern[]] := If[OptionValue@Squared, x x, x];
(* fff, another function to do something, with a different parameter *)
Options[fff] = {Cubed -> False};
fff[x_, OptionsPattern[]] := If[OptionValue@Cubed, x x x, x];
(* A set of functions you would like your function to be able to handle options for *)
myFuncs = {ff, fff, Plot};
(* Gather together the options from the set of functions you want your function to support,
plus the options for your own function itself *)
Options[f] =
Flatten[{Flatten[Options /@ myFuncs, 1], myOpt1 -> 7,
aFunction -> ff}, 1];
(* f, a function which has it's own options and can accept an arbitrary function
and associated options from the set of those functions who's options it
supports and returns some elements based on those options *)
f[p1_, func_, opts : OptionsPattern[]] := Module[{p, q, r},
(* Utilise the function's own, explicitly defined option, myOpt1 and calls a function,
which was passed as a parameter, along with it's valid options.
This is achieved using FilterRules *)
p = OptionValue[myOpt1] func[p1, FilterRules[{opts}, Options[func]]];
(* Call a function, passed as an option and any associted options it may
have which were passed into this function *)
q = OptionValue[aFunction][p1,
FilterRules[{opts}, Options[OptionValue[aFunction]]]];
(* Call Plot and filter any options given to this function which are supported by plot *)
r = Plot[
ff[x, FilterRules[{opts}, Options[OptionValue[aFunction]]]], {x,
0, 10}, Evaluate@FilterRules[{opts}, Options[Plot]]] ;
(* return the computed elements *)
{p, q, r}
]
(* An example function call, with options for ff, fff and Plot *)
f[5, fff, Frame -> True, Squared -> True, Cubed -> True]
myPlot[arg_, opt : OptionsPattern[]] := Graphics[{Circle[]}, FilterRules[{opt}, Options[Graphics]]]. With some functions you may need to precedeFilterRulewithSequence @@. – Szabolcs Mar 23 '17 at 15:42myPlot(which may be different form the default forGraphics)? – Szabolcs Mar 23 '17 at 15:46FilterRules[Options[myPlot], Options[Graphics]]at the end of the example above. It exploits the fact that options specified first take precedence. – Szabolcs Mar 23 '17 at 15:58