I want to write a function that takes options that are nested. For example, suppose the function has one main option, "method" which can take two values, "a" and "b". Also, when method is "a", it can take one sub-option, "depth", whereas, when the method is "b", the sub-option that can be specified is "totTime".
Basically, the options and suboptions that make sense are
method->"a", depth->5 or method->"b", totTime->10
One should not, for example, use the combination method->"a", and totTime->10.
One way to deal with this is
In[53]:= Options[f] = {method -> "a", depth -> 5, totTime -> 10}
Out[53]= {method -> "a", depth -> 5, totTime -> 10}
f[x_, OptionsPattern[]] :=
Module[
{},
If[OptionValue[method] == "a", {x, OptionValue[method],
OptionValue[depth]},
{x, OptionValue[method], OptionValue[totTime]}
]
]
We get proper output, despite "wrong" input
In[59]:= f[2, method -> "b", depth -> 6]
Out[59]= {2, "b", 10}
In[60]:= f[2, method -> "a", totTime -> 2]
Out[60]= {2, "a", 5}
However, the above set of options that the user specifies in the input to the function do not make sense as the sub-option, depth is not related to method "b". How can one make sure that the user does not enter nonsensical combinations of options and sub-options, without relying solely on the documentation.
I am looking for a method that works even if there are a ton of options and sub-options. That is, Ideally, I do not want to write a huge number of warnings saying that different combinations do not make sense. Someway to nudge the user toward proper input (e.g., sublists) would be useful.
Options
specified inIn[53]
not contradict your requirement that "method->"a", and totTime->10" are not allowed? The reasonIn[60]
returns {2,"a",5} is that you've provided the default value of 5 for"depth"
. $\endgroup$