OptionsPattern

OptionsPattern[]

is a pattern object that represents a collection of options given as rules, where the values of the options can be accessed using OptionValue.

OptionsPattern[f]

takes default option values from Options[f].

OptionsPattern[{opt1val1,opt2val2,}]

uses an explicit list of default option values.

Details

  • OptionsPattern matches any sequence or nested list of rules, specified with -> or :>, whose left-hand sides are symbols or strings.
  • In OptionsPattern[{spec1,spec2,}] the speci can be either heads fi or explicit rules opti->vali. Each head is treated as the list of rules obtained from Options[fi].
  • OptionsPattern[] uses the default options of the nearest enclosing function.
  • OptionsPattern[{}] includes no default options.

Examples

open allclose all

Basic Examples  (3)

Define default option values for the function f:

Define f, allowing options to be given:

Use f with an explicit option setting:

Use f with options taken to have their default values:

Define default option values for this assignment without explicitly setting up Options[f]:

Take default option values from Plot:

Use the defaults from Plot:

Override one of the defaults from Plot:

Scope  (4)

Use the options of ArcLength, but override the default value of Method:

Explicit option settings are honored:

f uses the same default as ArcLength for Assumptions, but not Method:

Take options from both Plot and Plot3D:

Give an explicit setting:

For common options, the default is inherited from Plot:

The function g inherits defaults for common options from Plot3D:

Use OptionsPattern with no default options:

Explicit uses of the option are honored:

The option name is returned if not explicitly present in the options:

OptionsPattern can be used in normal pattern-matching functions like ReplaceAll:

Applications  (2)

Define the default option values for a function f:

Implement f using a helper function fCore, but still inherit defaults from f:

The default option value of f is honored:

An explicit option settings takes precedence:

Define two functions with options:

Define a function h that uses both sets of options, giving preference to the defaults of g:

Properties & Relations  (3)

OptionsPattern matches any sequence of rules or lists of rules:

An empty sequence matches OptionsPattern:

An empty list matches OptionsPattern:

Possible Issues  (2)

OptionsPattern will match any set of options:

This may cause messages if invalid options are passed:

FilterRules can be used to only use options valid for f:

Any nesting of empty lists will match OptionsPattern:

Wolfram Research (2007), OptionsPattern, Wolfram Language function, https://reference.wolfram.com/language/ref/OptionsPattern.html.

Text

Wolfram Research (2007), OptionsPattern, Wolfram Language function, https://reference.wolfram.com/language/ref/OptionsPattern.html.

CMS

Wolfram Language. 2007. "OptionsPattern." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/OptionsPattern.html.

APA

Wolfram Language. (2007). OptionsPattern. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/OptionsPattern.html

BibTeX

@misc{reference.wolfram_2024_optionspattern, author="Wolfram Research", title="{OptionsPattern}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/OptionsPattern.html}", note=[Accessed: 21-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_optionspattern, organization={Wolfram Research}, title={OptionsPattern}, year={2007}, url={https://reference.wolfram.com/language/ref/OptionsPattern.html}, note=[Accessed: 21-December-2024 ]}