All predicatesShow sourceoption.pl -- Option list processing

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.

SWI-Prolog dicts provide a convenient and efficient alternative to option lists. For this reason, both built-in predicates and predicates that use this library support dicts transparantly.

Processing option lists inside time-critical code (loops) can cause serious overhead. The above mentioned dicts is the preferred mitigation. A more portable alternative 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.

Options typically have exactly one argument. The library does support options with 0 or more than one argument with the following restrictions:

See also
- library(record)
- Option processing capabilities may be declared using the directive predicate_options/3.
Source option(?Option, +Options) is semidet
Get an Option from Options. Fails silently if the option does not appear in Options. If Option appears multiple times in Options, the first value is used.
Arguments:
Option- Term of the form Name(?Value).
Options- is a list of Name(Value) or Name=Value or a dict.
Source option(?Option, +Options, +Default) is det
Get an Option from Options. If Option does not appear in Options, unify the value with Default. If Option appears multiple times in Options, the first value is used. For example
?- option(max_depth(D), [x(a), max_depth(20)], 10).
D = 20.
?- option(max_depth(D), [x(a)], 10).
D = 10.
Arguments:
Option- Term of the form Name(?Value).
Options- is a list of Name(Value) or Name=Value or a dict.
Source select_option(?Option, +Options, -RestOptions) is semidet
Get and remove Option from Options. As option/2, removing the matching option from Options and unifying the remaining options with RestOptions. If Option appears multiple times in Options, the first value is used. Note that if Options contains multiple terms that are compatible to Option, the first is used to set the value of Option and the duplicate appear in RestOptions.
Source select_option(?Option, +Options, -RestOptions, +Default) is det
Get and remove Option with default value. As select_option/3, but if Option is not in Options, its value is unified with Default and RestOptions with Options.
Source merge_options(+New, +Old, -Merged) is det
Merge two option sets. If Old is a dict, Merged is a dict. Otherwise Merged is a sorted list of options using the canonical format Name(Value) holding all options from New and Old, after removing conflicting options from Old.

Multi-values options (e.g., proxy(Host, Port)) are allowed, where both option-name and arity define the identity of the option.

Source canonicalise_options(+OptionsIn, -OptionsOut) is det[private]
Rewrite option list from possible Name=Value to Name(Value)
Source meta_options(+IsMeta, :Options0, -Options) is det
Perform meta-expansion on options that are module-sensitive. Whether an option name is module-sensitive is determined by calling 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.

To be done
- Should be integrated with declarations from predicate_options/3.
Source dict_options(?Dict, ?Options) is det
Convert between an option list and a dictionary. One of the arguments must be instantiated. If the option list is created, it is created in canonical form, i.e., using Option(Value) with the Options sorted in the standard order of terms. Note that the conversion is not always possible due to different constraints and conversion may thus lead to (type) errors.

Also note that most system predicates and predicates using this library for processing the option argument can both work with classical Prolog options and dicts objects.