Public API
This page lists exported symbols of CTBase.Options.
From CTBase.Options
CTBase.Options [Module]
CTBase.Options Module
Generic option handling for the Control Toolbox ecosystem.
This module provides the foundational types and functions for:
Option value tracking with provenance
Option schema definition with validation and aliases
Option extraction with alias support
Type validation and helpful error messages
The Options module is deliberately generic and has no dependencies on other CTBase modules, making it reusable across the ecosystem.
NotProvided [Constant]
CTBase.Options.NotProvided Constant
NotProvidedSingleton instance of NotProvidedType.
Use this as the default value in OptionDefinition to indicate that an option has no default value and should not be stored if not provided by the user.
Example
julia> using CTBase.Options
julia> def = OptionDefinition(
name = :optional_param,
type = Any,
default = NotProvided,
description = "Optional parameter"
)
julia> # If user doesn't provide it, it won't be stored
julia> opts, _ = extract_options((other=1,), [def])
julia> haskey(opts, :optional_param)
falseNotProvidedType [Struct]
CTBase.Options.NotProvidedType Type
struct NotProvidedTypeSingleton type representing the absence of a default value for an option.
This type is used to distinguish between:
default = NotProvided: No default value, option must be provided by user or not storeddefault = nothing: The default value is explicitlynothing
Example
# Option with no default - won't be stored if not provided
opt1 = OptionDefinition(
name = :minimize,
type = Union{Bool, Nothing},
default = NotProvided,
description = "Whether to minimize"
)
# Option with explicit nothing default - will be stored as nothing
opt2 = OptionDefinition(
name = :backend,
type = Union{Nothing, KernelAbstractions.Backend},
default = nothing,
description = "Execution backend"
)See also: OptionDefinition, extract_options
OptionDefinition [Struct]
CTBase.Options.OptionDefinition Type
struct OptionDefinition{T}Unified option definition for both option extraction and strategy contracts.
This type provides a comprehensive option definition that can be used for:
Option extraction in the Options module
Strategy contract definition in the Strategies module
Action schema definition
Fields
name::Symbol: Primary name of the optiontype::Type: Expected Julia type for the option valuedefault::T: Default value when the option is not provided (type parameterT)description::String: Human-readable description of the option's purposealiases::Tuple{Vararg{Symbol}}: Alternative names for this option (default: empty tuple)validator::Union{Function, Nothing}: Optional validation function (default:nothing)computed::Bool: Whether the default value is computed from parameters (default:false)
Type Parameter T
The type parameter T represents the type of the default value:
T = Anywhendefault = nothing(explicit nothing default)T = NotProvidedTypewhendefault = NotProvided(no default value)T = typeof(default)for concrete default values
Validator Contract
Validators must follow this pattern:
x -> condition || throw(ArgumentError("error message"))The validator should:
Return
true(or any truthy value) if the value is validThrow an exception (preferably
ArgumentError) if the value is invalidBe a pure function without side effects
Constructor Validation
The constructor performs the following validations:
Checks that
defaultmatches the specifiedtype(unlessdefaultisnothingorNotProvided)Runs the
validatoron thedefaultvalue (if both are provided anddefaultis notNotProvided)
Example
def = OptionDefinition(
name = :max_iter,
type = Int,
default = 100,
description = "Maximum number of iterations",
aliases = (:max, :maxiter),
validator = x -> x > 0 || throw(ArgumentError("$x must be positive"))
)
def.name # :max_iter
def.aliases # (:max, :maxiter)
all_names(def) # (:max_iter, :max, :maxiter)Throws
CTBase.Exceptions.IncorrectArgument: If the default value does not match the declared typeException: If the validator function fails when applied to the default value
See also: all_names, extract_option, extract_options, NotProvided
OptionValue [Struct]
CTBase.Options.OptionValue Type
struct OptionValue{T}Represents an option value with its source provenance.
Fields
value::T: The actual option value.source::Symbol: Where the value came from (:default,:user,:computed).
Constructor Validation
The constructor validates the source symbol using Val dispatch for compile-time type safety and performance optimization. Invalid sources throw Exceptions.IncorrectArgument.
Notes
The source field tracks the provenance of the option value:
:default: Value comes from the tool's default configuration:user: Value was explicitly provided by the user:computed: Value was computed/derived from other options
Example
julia> using CTBase.Options
julia> opt = OptionValue(100, :user)
OptionValue{Int64}(100, :user)
julia> opt.value
100
julia> opt.source
:userThrows
Exceptions.IncorrectArgument: If source is not one of:default,:user, or:computed
See also: value, source, is_user
aliases [Function]
CTBase.Options.aliases Function
aliases(
def::CTBase.Options.OptionDefinition
) -> Tuple{Vararg{Symbol}}Get the aliases for this option definition.
Returns
Tuple{Vararg{Symbol}}: Tuple of alias names
Example
julia> using CTBase.Options
julia> def = OptionDefinition(name=:max_iter, type=Int, default=100,
description="Maximum iterations",
aliases=(:max, :maxiter))
OptionDefinition{Int}(...)
julia> aliases(def)
(:max, :maxiter)See also: all_names, name
all_names [Function]
CTBase.Options.all_names Function
all_names(
def::CTBase.Options.OptionDefinition
) -> Tuple{Symbol, Vararg{Symbol}}Return all valid names for an option definition (primary name plus aliases).
This function is used by the extraction system to search for an option in kwargs using all possible names (primary name and all aliases).
Arguments
def::OptionDefinition: The option definition
Returns
Tuple{Vararg{Symbol}}: Tuple containing the primary name followed by all aliases
Example
julia> using CTBase.Options
julia> def = OptionDefinition(
name = :grid_size,
type = Int,
default = 100,
description = "Grid size",
aliases = (:n, :size)
)
OptionDefinition{Int}(...)
julia> all_names(def)
(:grid_size, :n, :size)See also: OptionDefinition, extract_option
default [Function]
CTBase.Options.default Function
default(def::CTBase.Options.OptionDefinition) -> AnyGet the default value for this option definition.
Returns
- The default value
Example
julia> using CTBase.Options
julia> def = OptionDefinition(name=:max_iter, type=Int, default=100,
description="Maximum iterations")
OptionDefinition{Int}(...)
julia> default(def)
100See also: name, type, is_required
description [Function]
CTBase.Options.description Function
description(def::CTBase.Options.OptionDefinition) -> StringGet the description for this option definition.
Returns
String: The option description
Example
julia> using CTBase.Options
julia> def = OptionDefinition(name=:max_iter, type=Int, default=100,
description="Maximum iterations")
OptionDefinition{Int}(...)
julia> description(def)
"Maximum iterations"See also: name, type
extract_option [Function]
CTBase.Options.extract_option Function
extract_option(
kwargs::NamedTuple,
def::CTBase.Options.OptionDefinition
) -> Tuple{Any, NamedTuple}Extract a single option from a NamedTuple using its definition, with support for aliases.
This function searches through all valid names (primary name + aliases) in the definition to find the option value in the provided kwargs. If found, it validates the value, checks the type, and returns an OptionValue with :user source. If not found, returns the default value with :default source.
Arguments
kwargs::NamedTuple: NamedTuple containing potential option values.def::OptionDefinition: Definition defining the option to extract.
Returns
(OptionValue, NamedTuple): Tuple containing the extracted option value and the remaining kwargs.
Notes
If a validator is provided in the definition, it will be called on the extracted value.
Validators should follow the pattern
x -> condition || throw(ArgumentError("message")).If validation fails, the original exception is rethrown after logging context with
@error.Type mismatches throw
Exceptions.IncorrectArgumentexceptions.The function removes the found option from the returned kwargs.
Throws
CTBase.Exceptions.IncorrectArgument: If type mismatch between value and definitionException: If validator function fails
Example
def = OptionDefinition(
name = :grid_size,
type = Int,
default = 100,
description = "Grid size",
aliases = (:n, :size)
)
kwargs = (n=200, tol=1e-6, max_iter=1000)
opt, remaining = extract_option(kwargs, def)
opt.value # 200
opt.source # :user
remaining.tol # 1e-6extract_options [Function]
CTBase.Options.extract_options Function
extract_options(
kwargs::NamedTuple,
defs::Vector{<:CTBase.Options.OptionDefinition}
) -> Tuple{Dict{Symbol, CTBase.Options.OptionValue}, NamedTuple}Extract multiple options from a NamedTuple using a vector of definitions.
This function iteratively applies extract_option for each definition in the vector, building a dictionary of extracted options while progressively removing processed options from the kwargs.
Arguments
kwargs::NamedTuple: NamedTuple containing potential option values.defs::Vector{OptionDefinition}: Vector of definitions defining options to extract.
Returns
(Dict{Symbol, OptionValue}, NamedTuple): Dictionary mapping option names to their values, and remaining kwargs.
Notes
The extraction order follows the order of definitions in the vector.
Each definition's primary name is used as the dictionary key.
Options not found in kwargs use their definition default values.
Validation is performed for each option using
extract_option.
Throws
- Any exception raised by validators in the definitions
See also: extract_option, OptionDefinition, OptionValue
Example
defs = [
OptionDefinition(name = :grid_size, type = Int, default = 100, description = "Grid size"),
OptionDefinition(name = :tol, type = Float64, default = 1e-6, description = "Tolerance")
]
kwargs = (grid_size=200, max_iter=1000)
options, remaining = extract_options(kwargs, defs)
options[:grid_size].value # 200
options[:tol].value # 1e-6
remaining.max_iter # 1000extract_options(
kwargs::NamedTuple,
defs::NamedTuple
) -> Tuple{NamedTuple, NamedTuple}Extract multiple options from a NamedTuple using a NamedTuple of definitions.
This function is similar to the Vector version but returns a NamedTuple instead of a Dict for convenience when the definition structure is known at compile time.
Arguments
kwargs::NamedTuple: NamedTuple containing potential option values.defs::NamedTuple: NamedTuple of definitions defining options to extract.
Returns
(NamedTuple, NamedTuple): NamedTuple of extracted options and remaining kwargs.
Notes
The extraction order follows the order of definitions in the NamedTuple.
Each definition's primary name is used as the key in the returned NamedTuple.
Options not found in kwargs use their definition default values.
Validation is performed for each option using
extract_option.
Throws
- Any exception raised by validators in the definitions
See also: extract_option, OptionDefinition, OptionValue
Example
defs = (
grid_size = OptionDefinition(name = :grid_size, type = Int, default = 100, description = "Grid size"),
tol = OptionDefinition(name = :tol, type = Float64, default = 1e-6, description = "Tolerance")
)
kwargs = (grid_size=200, max_iter=1000)
options, remaining = extract_options(kwargs, defs)
options.grid_size.value # 200
options.tol.value # 1e-6
remaining.max_iter # 1000extract_raw_options [Function]
CTBase.Options.extract_raw_options Function
extract_raw_options(options::NamedTuple) -> NamedTupleExtract raw option values from a NamedTuple of options, unwrapping OptionValue wrappers and filtering out NotProvided values.
This utility function is useful when passing options to external builders or functions that expect plain keyword arguments without OptionValue wrappers or undefined options.
Options with NotProvided values are excluded from the result, allowing external builders to use their own defaults. Options with explicit nothing values are included.
Arguments
options::NamedTuple: NamedTuple containing option values (may be wrapped in OptionValue)
Returns
NamedTuple: NamedTuple with unwrapped values, excluding anyNotProvidedvalues
Example
opts = (backend = OptionValue(:optimized, :user),
show_time = OptionValue(false, :default),
minimize = OptionValue(nothing, :default),
optional = OptionValue(NotProvided, :default))
extract_raw_options(opts)
# (backend = :optimized, show_time = false, minimize = nothing)See also: OptionValue, extract_options, NotProvided
has_default [Function]
CTBase.Options.has_default Function
has_default(def::CTBase.Options.OptionDefinition) -> BoolCheck if this option definition has a default value.
Returns false when the default value is NotProvided.
Returns
Bool:trueif a default value is defined
Example
julia> using CTBase.Options
julia> def = OptionDefinition(name=:max_iter, type=Int, default=100,
description="Maximum iterations")
OptionDefinition{Int}(...)
julia> has_default(def)
trueSee also: is_required, default
has_validator [Function]
CTBase.Options.has_validator Function
has_validator(def::CTBase.Options.OptionDefinition) -> BoolCheck if this option definition has a validator function.
Returns
Bool:trueif a validator is defined
Example
julia> using CTBase.Options
julia> def = OptionDefinition(name=:max_iter, type=Int, default=100,
description="Maximum iterations",
validator=x -> x > 0)
OptionDefinition{Int}(...)
julia> has_validator(def)
trueSee also: validator, name
is_computed [Function]
CTBase.Options.is_computed Function
is_computed(opt::CTBase.Options.OptionValue) -> BoolCheck if this option value was computed from other options.
Returns
Bool:trueif the source is:computed
Example
opt = OptionValue(100, :computed)
is_computed(opt) # trueSee also: is_user, is_default, source
is_computed(def::CTBase.Options.OptionDefinition) -> BoolCheck if this option definition has a computed default value.
Returns true when the default value is computed from strategy parameters (e.g., backend in Exa{GPU} which depends on the GPU parameter).
Returns
Bool:trueif the default is computed from parameters
Example
julia> using CTBase.Options
julia> # Static default
julia> def1 = OptionDefinition(name=:max_iter, type=Int, default=100,
description="Maximum iterations")
OptionDefinition{Int}(...)
julia> is_computed(def1)
false
julia> # Computed default
julia> def2 = OptionDefinition(name=:backend, type=Any, default=compute_backend(),
description="Backend", computed=true)
OptionDefinition{...}(...)
julia> is_computed(def2)
trueSee also: has_default, is_required, OptionDefinition
is_computed(
opts::CTBase.Strategies.StrategyOptions,
key::Symbol
) -> BoolCheck if an option was computed.
Arguments
opts::StrategyOptions: Strategy optionskey::Symbol: Option name
Returns
Bool:trueif the option was computed
Example
julia> Options.is_computed(opts, :step)
trueSee also: Options.source, Options.is_user, Options.is_default
is_default [Function]
CTBase.Options.is_default Function
is_default(opt::CTBase.Options.OptionValue) -> BoolCheck if this option value is using its default.
Returns
Bool:trueif the source is:default
Example
opt = OptionValue(100, :default)
is_default(opt) # trueSee also: is_user, is_computed, source
is_default(
opts::CTBase.Strategies.StrategyOptions,
key::Symbol
) -> BoolCheck if an option is using its default value.
Arguments
opts::StrategyOptions: Strategy optionskey::Symbol: Option name
Returns
Bool:trueif the option is using its default value
Example
julia> Options.is_default(opts, :tol)
trueSee also: Options.source, Options.is_user, Options.is_computed
is_required [Function]
CTBase.Options.is_required Function
is_required(def::CTBase.Options.OptionDefinition) -> BoolCheck if this option is required (has no default value).
Returns true when the default value is NotProvided.
Returns
Bool:trueif the option is required
Example
julia> using CTBase.Options
julia> def = OptionDefinition(name=:input, type=String, default=NotProvided,
description="Input file")
OptionDefinition{NotProvidedType}(...)
julia> is_required(def)
trueSee also: has_default, default
is_user [Function]
CTBase.Options.is_user Function
is_user(opt::CTBase.Options.OptionValue) -> BoolCheck if this option value was explicitly provided by the user.
Returns
Bool:trueif the source is:user
Example
opt = OptionValue(100, :user)
is_user(opt) # trueSee also: is_default, is_computed, source
is_user(
opts::CTBase.Strategies.StrategyOptions,
key::Symbol
) -> BoolCheck if an option was provided by the user.
Arguments
opts::StrategyOptions: Strategy optionskey::Symbol: Option name
Returns
Bool:trueif the option was provided by the user
Example
julia> Options.is_user(opts, :max_iter)
trueSee also: Options.source, Options.is_default, Options.is_computed
name [Function]
CTBase.Options.name Function
name(def::CTBase.Options.OptionDefinition) -> SymbolGet the primary name of this option definition.
Returns
Symbol: The option name
Example
julia> using CTBase.Options
julia> def = OptionDefinition(name=:max_iter, type=Int, default=100,
description="Maximum iterations")
OptionDefinition{Int}(...)
julia> name(def)
:max_iterSee also: type, default, aliases
source [Function]
CTBase.Options.source Function
source(opt::CTBase.Options.OptionValue) -> SymbolGet the source provenance of this option value.
Returns
Symbol::default,:user, or:computed
Example
opt = OptionValue(100, :user)
source(opt) # :userSee also: value, is_user
source(
opts::CTBase.Strategies.StrategyOptions,
key::Symbol
) -> SymbolGet the source of an option.
Arguments
opts::StrategyOptions: Strategy optionskey::Symbol: Option name
Returns
Symbol: Source of the option (:user,:default, or:computed)
Example
julia> Options.source(opts, :max_iter)
:userSee also: Options.is_user, Options.is_default, Options.is_computed
type [Function]
CTBase.Options.type Function
type(def::CTBase.Options.OptionDefinition) -> TypeGet the expected type for this option definition.
Returns
Type: The expected type
Example
julia> using CTBase.Options
julia> def = OptionDefinition(name=:max_iter, type=Int, default=100,
description="Maximum iterations")
OptionDefinition{Int}(...)
julia> type(def)
IntSee also: name, default
validator [Function]
CTBase.Options.validator Function
validator(
def::CTBase.Options.OptionDefinition
) -> Union{Nothing, Function}Get the validator function for this option definition.
Returns
Union{Function, Nothing}: The validator function ornothing
Example
julia> using CTBase.Options
julia> validator_fn = x -> x > 0
julia> def = OptionDefinition(name=:max_iter, type=Int, default=100,
description="Maximum iterations",
validator=validator_fn)
OptionDefinition{Int}(...)
julia> validator(def) === validator_fn
trueSee also: has_validator, name
value [Function]
CTBase.Options.value Function
value(opt::CTBase.Options.OptionValue) -> AnyGet the value from this option value wrapper.
Returns
- The stored option value
Example
opt = OptionValue(100, :user)
value(opt) # 100See also: source, is_user
value(
opts::CTBase.Strategies.StrategyOptions,
key::Symbol
) -> AnyGet the value of an option.
Arguments
opts::StrategyOptions: Strategy optionskey::Symbol: Option name
Returns
Any: Value of the option
Example
julia> Options.value(opts, :max_iter)
200See also: Options.is_user, Options.is_default, Options.is_computed