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.

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.

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

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.

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.

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

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

option.

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.

In the next cell PlotFunction performs as expected when given no options, one

option, or two options.

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.

Unfortunately our PlotFunction defined above has a problem when a list of

option is given in the next cell.

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.

The next cell demonstrates that the new version of PlotFunction can take any

way of expressing options that passes the OptionQ test.

Created by Mathematica (May 16, 2004)