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
    Nonetreat 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 allclose all

Basic Examples  (1)

Declare the options of the function f:

Verify that f is called with exactly one positional argument and known options:

Appropriate messages are issued if the arguments to f are invalid:

Scope  (6)

Check the arguments for a function that takes between 1 and 3 positional arguments:

Require at least 2 positional arguments:

Calling f with 1 argument is invalid:

Require exactly 3 positional arguments:

Allow "hidden" options that do not appear in Options[f]:

The option a0 is not a known option for f:

Allow the option named hidden as well as any option of Graphics to be set:

Treat unknown trailing options in positions min+1 though max as positional arguments rather than options for f:

The rule b2 is treated as a positional argument:

With the default "OptionsMode""Longest", a message is issued for unknown options:

A known option is still treated as an option for f with "OptionsMode""Shortest":

Treat all elements as positional arguments rather than options for f:

Applications  (2)

Define a function that takes two arguments:

Issue a message for any other number of arguments and return unevaluated:

The following evaluates normally:

With an incorrect argument count, a message is issued:

Define a function that calls a helper function if called with at most one argument:

When called with at most one argument, the helper function is called:

When called with more than one argument, f returns unevaluated and issues a message:

Properties & Relations  (8)

CheckArguments[f[],] issues a message for f and returns False for invalid input to f:

If f does not accept options, all arguments are assumed to be positional:

CheckArguments[,{min,max}] will always treat the first min arguments as positional:

Here, even though b0 matches OptionsPattern[], it is treated as a positional argument:

If the rule b0 is given after the positional arguments, it is treated as an unknown option:

Trailing rules matching OptionsPattern[] are treated as options if they are known options to f:

This is true even if "OptionsMode""Shortest" is given:

An option specification that contains both known and unknown options generates a message:

In "OptionsMode""Shortest", a list with known and unknown options is treated as a positional argument if possible:

An unknown option past the maximum number of positional arguments still generates a message:

Only rules matching OptionsPattern[] can be valid option specifications:

The rule 12 does not match OptionsPattern[]:

Thus it is treated as a positional argument:

If 12 is given after the positional arguments, it is treated as an invalid option specification:

CheckArguments returns False when ArgumentsOptions returns Failure[]:

Possible Issues  (2)

All arguments at or before the minimum argument count are considered positional arguments:

IgnoreCaseTrue is treated as an argument despite being an option to StringCases:

CheckArguments does not check whether the option values are correct:

Infinity is not a valid value for Heads:

Wolfram Research (2020), CheckArguments, Wolfram Language function, https://reference.wolfram.com/language/ref/CheckArguments.html (updated 2024).

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_2024_checkarguments, author="Wolfram Research", title="{CheckArguments}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/CheckArguments.html}", note=[Accessed: 22-December-2024 ]}

BibLaTeX

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