The library(option) provides some utilities for
processing option lists. Option lists are commonly used as an
alternative for many arguments. Examples of built-in predicates are open/4
and write_term/3.
Naming the arguments results in more readable code, and the list nature
makes it easy to extend the list of options accepted by a predicate.
Option lists come in two styles, both of which are handled by this
library.
Processing options inside time-critical code (loops) can cause
serious overhead. One possibility is to define a record using library(record)
and initialise this using make_<record>/2.
In addition to providing good performance, this also provides
type-checking and central declaration of defaults.
:- record atts(width:integer=100, shape:oneof([box,circle])=box).
process(Data, Options) :-
make_atts(Options, Attributes),
action(Data, Attributes).
action(Data, Attributes) :-
atts_shape(Attributes, Shape),
...
Options typically have exactly one argument. The library does support options with 0 or more than one argument with the following restrictions:
arg(1, Option, Default),
causing failure without arguments and filling only the first
option-argument otherwise.
| Option | Term of the form Name(?Value). |
| Option | Term of the form Name(?Value). |
Multi-values options (e.g., proxy(Host, Port)) are
allowed, where both option-name and arity define the identity of the
option.
call(IsMeta, Name).
Here is an example:
meta_options(is_meta, OptionsIn, Options),
...
is_meta(callback).
Meta-options must have exactly one argument. This argument will be qualified.