CheckArguments[f[args],n]
gives True if args consists of exactly n positional arguments followed by valid options for f, and False otherwise.
CheckArguments[f[args],{min,max}]
requires the number of positional arguments to be between min and max.
CheckArguments[f[args],spec,assoc]
modifies the behavior based on the information in the association assoc.
CheckArguments
CheckArguments[f[args],n]
gives True if args consists of exactly n positional arguments followed by valid options for f, and False otherwise.
CheckArguments[f[args],{min,max}]
requires the number of positional arguments to be between min and max.
CheckArguments[f[args],spec,assoc]
modifies the behavior based on the information in the association assoc.
Details
- CheckArguments issues appropriate messages for f and returns False if f[…] has the wrong number of positional arguments, unknown options or invalid option specifications.
- The following keys can be used for the association assoc:
-
"ExtraOptions" {} additional options to treat as known options for f "OptionsMode" "Longest" how to interpret the optional positional arguments of args - Additional options given to "ExtraOptions" can be specified as rules nameval or as symbols func, which is equivalent to Options[func].
- CheckArguments checks whether all options specified for f have valid names.
- The following values of "OptionsMode" can be used: »
-
"Longest" treat all trailing elements of args matching OptionsPattern[] as options for f "Shortest" treat anything other than one or more known options as positional arguments None treat all elements as positional arguments - Even with "OptionsMode""Shortest", an unknown option past position max still generates a message. »
- CheckArguments has the attribute HoldFirst.
Examples
open all close allBasic Examples (1)
Declare the options of the function f:
Options[f] = {a -> 1}Verify that f is called with exactly one positional argument and known options:
CheckArguments[f[1, a -> 0], 1]Appropriate messages are issued if the arguments to f are invalid:
CheckArguments[f[1, 2], 1]Scope (6)
Check the arguments for a function that takes between 1 and 3 positional arguments:
Options[f] = {a -> 1}CheckArguments[f[1, 2, 3, a -> 0], {1, 3}]Require at least 2 positional arguments:
CheckArguments[f[1, 2, 3], {2, ∞}]Calling f with 1 argument is invalid:
CheckArguments[f[1], {2, ∞}]Require exactly 3 positional arguments:
Options[f] = {a -> 1, b -> 2}CheckArguments[f[1, 2, 3, a -> 0, b -> 0], 3]Allow "hidden" options that do not appear in Options[f]:
CheckArguments[f[1, a -> 0], 1, <|"ExtraOptions" -> {a -> 1}|>]The option a0 is not a known option for f:
Options[f]Allow the option named hidden as well as any option of Graphics to be set:
Options[f] = {normal -> Automatic}CheckArguments[f[1, normal -> 3, hidden -> 2, AspectRatio -> 1], {1, 2}, <|"ExtraOptions" -> {hidden -> 0, Graphics}|>]Treat unknown trailing options in positions min+1 though max as positional arguments rather than options for f:
Options[f] = {a -> 1}The rule b2 is treated as a positional argument:
CheckArguments[f[1, b -> 2], {1, 2}, <|"OptionsMode" -> "Shortest"|>]With the default "OptionsMode""Longest", a message is issued for unknown options:
CheckArguments[f[1, b -> 2], {1, 2}, <|"OptionsMode" -> "Longest"|>]A known option is still treated as an option for f with "OptionsMode""Shortest":
CheckArguments[f[a -> 2], 0, <|"OptionsMode" -> "Shortest"|>]Treat all elements as positional arguments rather than options for f:
CheckArguments[f[a -> 2], 0, <|"OptionsMode" -> None|>]Applications (2)
Define a function that takes two arguments:
f[x_, y_] := 2x yIssue a message for any other number of arguments and return unevaluated:
f[args___] := Null /; CheckArguments[f[args], 2]The following evaluates normally:
f[3, 4]With an incorrect argument count, a message is issued:
f[2]Define a function that calls a helper function if called with at most one argument:
f[args___] := g[args] /; CheckArguments[f[args], {0, 1}]g[] := 5
g[x_] := x + 5When called with at most one argument, the helper function is called:
f[]f[1]When called with more than one argument, f returns unevaluated and issues a message:
f[1, 2]Properties & Relations (8)
CheckArguments[f[…],…] issues a message for f and returns False for invalid input to f:
Options[f] = {a -> 1}CheckArguments[f[1, 2], 3]CheckArguments[f[0, b -> 2], 1]If f does not accept options, all arguments are assumed to be positional:
Options[f]CheckArguments[f[0, b -> 2], 1]CheckArguments[…,{min,max}] will always treat the first min arguments as positional:
Options[f] = {a -> 1}Here, even though b0 matches OptionsPattern[], it is treated as a positional argument:
CheckArguments[f[1, b -> 0], {2, 3}]If the rule b0 is given after the positional arguments, it is treated as an unknown option:
CheckArguments[f[1, 2, b -> 0], {2, 3}]Trailing rules matching OptionsPattern[] are treated as options if they are known options to f:
Options[f] = {a -> 1}This is true even if "OptionsMode""Shortest" is given:
CheckArguments[f[1, a -> 2], {1, 2}, <|"RulesMode" -> "Shortest"|>]An option specification that contains both known and unknown options generates a message:
Options[f] = {a -> 1}CheckArguments[f[1, {a -> 2, b -> 3}], 1]In "OptionsMode""Shortest", a list with known and unknown options is treated as a positional argument if possible:
Options[f] = {a -> 1}CheckArguments[f[1, {a -> 2, b -> 3}], {1, 2}, <|"OptionsMode" -> "Shortest"|>]An unknown option past the maximum number of positional arguments still generates a message:
CheckArguments[f[1, 2, {a -> 2, b -> 3}], {1, 2}, <|"OptionsMode" -> "Shortest"|>]Only rules matching OptionsPattern[] can be valid option specifications:
Options[f] = {a -> 1}The rule 12 does not match OptionsPattern[]:
MatchQ[1 -> 2, OptionsPattern[]]Thus it is treated as a positional argument:
CheckArguments[f[1 -> 2], {0, 1}]If 12 is given after the positional arguments, it is treated as an invalid option specification:
CheckArguments[f[0, 1 -> 2], {0, 1}]CheckArguments returns False when ArgumentsOptions returns Failure[…]:
CheckArguments[f[], 1]ArgumentsOptions[f[], 1]Possible Issues (2)
All arguments at or before the minimum argument count are considered positional arguments:
CheckArguments[StringCases["str", IgnoreCase -> True, Overlaps -> True], {2, 3}]IgnoreCaseTrue is treated as an argument despite being an option to StringCases:
Options[StringCases]CheckArguments does not check whether the option values are correct:
CheckArguments[Map[f, x, Heads -> ∞], {2, 3}]Infinity is not a valid value for Heads:
Map[f, x, Heads -> ∞]See Also
Tech Notes
Related Guides
Related Workflows
- Define a Function with Options
Text
Wolfram Research (2020), CheckArguments, Wolfram Language function, https://reference.wolfram.com/language/ref/CheckArguments.html (updated 2024).
CMS
Wolfram Language. 2020. "CheckArguments." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/CheckArguments.html.
APA
Wolfram Language. (2020). CheckArguments. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CheckArguments.html
BibTeX
@misc{reference.wolfram_2026_checkarguments, author="Wolfram Research", title="{CheckArguments}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/CheckArguments.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_checkarguments, organization={Wolfram Research}, title={CheckArguments}, year={2024}, url={https://reference.wolfram.com/language/ref/CheckArguments.html}, note=[Accessed: 12-June-2026]}