Options, OptionQ

OptionQ details

OptionQ isn't documented in The Mathematica Book or the Help Browser, but it's helpful when defining a function with  options.  OptionQ does however have the usage message shown below.

? OptionQ

OptionQ[e] returns True if e can be considered an option or list of  options, and False otherwise.


In the cells below we see OptionQ is True when it's given a Rule, a
RuleDelayed or a list of these things. You get an error message if you give
OptionQ more than one argument.

ClearAll["Global`*"] ;    OptionQ[opt1True]    OptionQ[opt2var2]    OptionQ[{opt115, opt2Automatic, opt3False, opt4All}]

True

True

True


OptionQ also returns True when it's given nested lists of options.

OptionQ[{{opt115, {opt210}, opt3False, opt4All}}]

True


As indicated in the usage message OptionQ returns False when it's given a
single argument with any other form.  This is seen in the next cell.

OptionQ[2]  OptionQ[h[2]]  OptionQ[{Opt1All, 2}]  OptionQ[{Opt1All, h[2]}]  OptionQ[h[Opt1All]]

False

False

False

False

False

Typical use of OptionQ

The next cell defines a function that takes options.  This function takes  one required argument and optional arguments are allowed as long as they all  pass OptionQ.  The form (opts___?OptionQ) is perhaps the most frequent use of  BlankNullSequence ( ___  ie. "Tripple Blank").  Notice this allows for a single option, a  sequence of options, or no options.

foo[expr_, opts___ ? OptionQ] := {expr, h @@ Flatten[{opts}]}


Each usage of foo in the next cell matches the definition of foo above.

foo[x + y]  foo[x + y, opt115, opt2Automatic, opt3False] ɯ ... False}]  foo[x + y, {opt115, {opt2Automatic}}, opt3False]

{x + y, h[]}

{x + y, h[opt115, opt2Automatic, opt3False]}

{x + y, h[opt115, opt2Automatic, opt3False]}

{x + y, h[opt115, opt2Automatic, opt3False]}


The definition of foo doesn't apply to the next example because 6  isn't an
option.

foo[x + y, opt1True, 6, opt2False]

foo[x + y, opt1True, 6, opt2False]

Getting the setting of Options

Section  2.3.10 of The Mathematica Book suggests using the form
    name/.{opts}/.Options[f]
to get the setting of an option.  This technique is used below to get  the PlotStyle and Axes settings for a function.

ClearAll[PlotFunction, x] ;    Options[PlotFunction] = {PlotStyleGrayLevel[0], Ima ... ;PlotStyle="<>ToString[style], "ImageSize="<>ToString[size]] ]


In the next cell PlotFunction performs as expected when given no options, one
option, or two options.

PlotFunction[x^2] <br /> PlotFunction[x^2, PlotStyleThickness[0.03]] <br /> PlotFunction[x^2, PlotStyleThickness[0.03], ImageSize {xx, yy}]

MakeGraphic[x^2, PlotStyle=GrayLevel[0], ImageSize=Automatic]

MakeGraphic[x^2, PlotStyle=Thickness[0.03], ImageSize=Automatic]

MakeGraphic[x^2, PlotStyle=Thickness[0.03], ImageSize={xx, yy}]


In the next cell Plot alows us to provide a list of options rather than a
sequence of options.  For consistency we would like our PlotFunction to allow
us to give a list of options as well.

Plot[Sin[x], {x, 0, 12}, {PlotStyleThickness[0.03], ImageSize {400, 250}}] ;


Unfortunately our PlotFunction defined above has a problem when a list of
option is given in the next cell.

PlotFunction[x^2, {PlotStyleThickness[0.03], ImageSize {400, 250}}]

Set :: shape : Lists  {style$38, size$38} and {{Thickness[0.03`], {400, 250}}} are not the same shape.

MakeGraphic[x^2, PlotStyle=style$38, ImageSize=size$38]

The way to avoid the problem in the last exaple is to use a different  method for getting the option settings.  Instead the following form should  normally be used to get option settings.
    {var1,var2,var3}={opt1,opt2,opt3}/.Flatten[{opts,Options[f]}]
This more robust method is used in the next cell.

ClearAll[PlotFunction] ;    Options[PlotFunction] = {PlotStyleGrayLevel[0], ImageS ... ;PlotStyle="<>ToString[style], "ImageSize="<>ToString[size]] ]


The next cell demonstrates that the new version of PlotFunction can take any
way of expressing options that passes the OptionQ test.

PlotFunction[x^2] <br /> PlotFunction[x^2, PlotStyleThickness[0.03]] <br /> PlotFuncti ... otStyleGrayLevel[0], {{ImageSizeAutomatic}, Ticks->Automatic}, Axes->True}]

MakeGraphic[x^2, PlotStyle=GrayLevel[0], ImageSize=Automatic]

MakeGraphic[x^2, PlotStyle=Thickness[0.03], ImageSize=Automatic]

MakeGraphic[x^2, PlotStyle=Thickness[0.03], ImageSize={xx, yy}]

MakeGraphic[x^2, PlotStyle=Thickness[0.03], ImageSize={400, 250}]

MakeGraphic[x^2, PlotStyle=GrayLevel[0], ImageSize=Automatic]


Created by Mathematica  (May 16, 2004)