ArgumentsOptions

ArgumentsOptions[f[args],n]

tries to separate args into a list of n positional arguments followed by a list of valid options for f.

ArgumentsOptions[f[args],{min,max}]

requires the number of positional arguments to be between min and max.

ArgumentsOptions[f[args],spec,assoc]

modifies the behavior based on the information in the association assoc.

Details

  • ArgumentsOptions issues appropriate messages for f and returns a Failure object 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
    "Head"Listwrapper for the positional arguments and options lists
  • Additional options given to "ExtraOptions" can be specified as rules nameval or as symbols func, which is equivalent to Options[func].
  • ArgumentsOptions 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. »
  • ArgumentsOptions has the attribute HoldFirst.

Examples

open allclose all

Basic Examples  (1)

Declare the options of the function f:

Separate the arguments to f into exactly one positional argument and options:

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

Scope  (7)

Separate the positional arguments and options 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 b1 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:

Separate the arguments without evaluating them:

Applications  (2)

Define a function that calls a helper function if called with one or two positional arguments:

When called with one or two positional arguments, the helper function is called:

When called with more than two positional arguments, f returns unevaluated and issues a message:

Use "OptionsMode""Shortest" to treat trailing empty lists as positional arguments:

Properties & Relations  (10)

ArgumentsOptions[f[],] issues a message and returns Failure[] for invalid input to f:

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

Options are always returned as a flat list, irrespective of how they appeared in the input expression:

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

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

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

Only trailing options are considered when collecting options:

Trailing rules matching OptionsPattern[] are treated as options if they are known options for 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:

ArgumentsOptions returns Failure[] when CheckArguments returns False:

Possible Issues  (3)

If f accepts options, trailing empty lists are ignored by default because {} matches OptionsPattern[]:

Use "OptionsMode""Shortest" to treat a trailing empty list as a positional argument:

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

ArgumentsOptions does not check whether the option values are correct:

Infinity is not a valid value for Heads:

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

Text

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

CMS

Wolfram Language. 2020. "ArgumentsOptions." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/ArgumentsOptions.html.

APA

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

BibTeX

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

BibLaTeX

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