Skip to content

Options Module

This section documents the options components of the Nextmv Python SDK.

options

Configuration management for application runs.

This module provides classes for handling configuration options for applications. It supports reading options from command-line arguments, environment variables, and default values in a prioritized manner. The module includes classes for defining individual options (Option) and managing collections of options (Options).

CLASS DESCRIPTION
Option

Class for defining individual options for configuration.

Options

Class for managing collections of options.

Option dataclass

Option(
    name: str,
    option_type: type,
    default: Optional[Any] = None,
    description: Optional[str] = None,
    required: bool = False,
    choices: Optional[list[Any]] = None,
    additional_attributes: Optional[dict[str, Any]] = None,
)

An option that is used in Options.

You can import the Option class directly from nextmv:

from nextmv import Option

Options provide a way to configure application behavior. When an Option is required, it is a good practice to provide a default value for it. This is because the Options will raise an error if a required Option is not provided through a command-line argument, an environment variable or a default value.

PARAMETER DESCRIPTION

name

name. The name of the option.

TYPE: str

option_type

The type of the option.

TYPE: type

default

The default value of the option. Even though this is optional, it is recommended to provide a default value for all options.

TYPE: Any DEFAULT: None

description

An optional description of the option. This is useful for generating help messages for the Options.

TYPE: str DEFAULT: None

required

Whether the option is required. If an option is required, it will be an error to not provide a value for it, either through a command-line argument, an environment variable or a default value.

TYPE: bool DEFAULT: False

choices

Limits values to a specific set of choices.

TYPE: list[Optional[Any]] DEFAULT: None

additional_attributes

Optional additional attributes for the option. The Nextmv Cloud may perform validation on these attributes. For example, the maximum length of a string or the maximum value of an integer. These additional attributes will be shown in the help message of the Options.

TYPE: dict[str, Any] DEFAULT: None

Examples:

from nextmv.options import Option
opt = Option("duration", str, "30s", description="solver duration", required=False)
opt.name
opt.default

additional_attributes class-attribute instance-attribute

additional_attributes: Optional[dict[str, Any]] = None

Optional additional attributes for the option. The Nextmv Cloud may perform validation on these attributes. For example, the maximum length of a string or the maximum value of an integer. These additional attributes will be shown in the help message of the Options.

choices class-attribute instance-attribute

choices: Optional[list[Any]] = None

Limits values to a specific set of choices.

default class-attribute instance-attribute

default: Optional[Any] = None

The default value of the option. Even though this is optional, it is recommended to provide a default value for all options.

description class-attribute instance-attribute

description: Optional[str] = None

An optional description of the option. This is useful for generating help messages for the Options.

from_dict classmethod

from_dict(data: dict[str, Any]) -> Option

Creates an instance of Option from a dictionary.

PARAMETER DESCRIPTION
data

The dictionary representation of an option.

TYPE: dict[str, Any]

RETURNS DESCRIPTION
Option

An instance of Option.

Examples:

>>> opt_dict = {"name": "timeout", "option_type": "<class 'int'>", "default": 60}
>>> option = Option.from_dict(opt_dict)
>>> option.name
'timeout'
>>> option.default
60
Source code in nextmv/nextmv/options.py
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "Option":
    """
    Creates an instance of `Option` from a dictionary.

    Parameters
    ----------

    data: dict[str, Any]
        The dictionary representation of an option.

    Returns
    -------
    Option
        An instance of `Option`.

    Examples
    --------
    >>> opt_dict = {"name": "timeout", "option_type": "<class 'int'>", "default": 60}
    >>> option = Option.from_dict(opt_dict)
    >>> option.name
    'timeout'
    >>> option.default
    60
    """

    option_type_string = data["option_type"]
    option_type = getattr(builtins, option_type_string.split("'")[1])

    return cls(
        name=data["name"],
        option_type=option_type,
        default=data.get("default"),
        description=data.get("description"),
        required=data.get("required", False),
        choices=data.get("choices"),
        additional_attributes=data.get("additional_attributes"),
    )

name instance-attribute

name: str

The name of the option.

option_type instance-attribute

option_type: type

The type of the option.

required class-attribute instance-attribute

required: bool = False

Whether the option is required. If a option is required, it will be an error to not provide a value for it, either trough a command-line argument, an environment variable or a default value.

to_dict

to_dict() -> dict[str, Any]

Converts the option to a dict.

RETURNS DESCRIPTION
dict[str, Any]

The option as a dict with all its attributes.

Examples:

>>> opt = Option("duration", str, "30s", description="solver duration")
>>> opt_dict = opt.to_dict()
>>> opt_dict["name"]
'duration'
>>> opt_dict["default"]
'30s'
Source code in nextmv/nextmv/options.py
def to_dict(self) -> dict[str, Any]:
    """
    Converts the option to a dict.

    Returns
    -------
    dict[str, Any]
        The option as a dict with all its attributes.

    Examples
    --------
    >>> opt = Option("duration", str, "30s", description="solver duration")
    >>> opt_dict = opt.to_dict()
    >>> opt_dict["name"]
    'duration'
    >>> opt_dict["default"]
    '30s'
    """

    return {
        "name": self.name,
        "option_type": str(self.option_type),
        "default": self.default,
        "description": self.description,
        "required": self.required,
        "choices": self.choices,
        "additional_attributes": self.additional_attributes,
    }

Options

Options(*options: Option)

Options container for application configuration.

You can import the Options class directly from nextmv:

from nextmv import Options

To initialize options, pass in one or more Option objects. The options will look for the values of the given parameters in the following order: command-line arguments, environment variables, default values.

Once the Options are initialized, you can access the underlying options as attributes of the Options object. For example, if you have an Option object with the name "duration", you can access it as options.duration.

If an option is required and not provided through a command-line argument, an environment variable or a default value, an error will be raised.

Options works as a Namespace, so you can assign new attributes to it. For example, you can do options.foo = "bar".

Options are parsed from the given sources when an attribute is accessed. Alternatively, you can call the parse method to parse the options manually. Options that are not parsed may be merged with other unparsed options, by using the merge method. Once options are parsed, they cannot be merged with other options. After options are parsed, you may get the help message by running the script with the -h/--help flag.

PARAMETER DESCRIPTION

*options

The list of Option objects that are used in the options. At least one option is required.

TYPE: Option DEFAULT: ()

Examples:

>>> import nextmv
>>>
>>> options = nextmv.Options(
...     nextmv.Option("duration", str, "30s", description="solver duration", required=False),
...     nextmv.Option("threads", int, 4, description="computer threads", required=False),
... )
>>>
>>> print(options.duration, options.threads, options.to_dict())
30s 4 {"duration": "30s", "threads": 4}
RAISES DESCRIPTION
ValueError

If a required option is not provided through a command-line argument, an environment variable or a default value.

TypeError

If an option is not either an Option or Parameter (deprecated) object.

ValueError

If an environment variable is not of the type of the corresponding parameter.

Initialize an Options instance with the provided option objects.

PARAMETER DESCRIPTION

*options

The option objects to include in this Options instance.

TYPE: Option DEFAULT: ()

Source code in nextmv/nextmv/options.py
def __init__(self, *options: Option):
    """
    Initialize an Options instance with the provided option objects.

    Parameters
    ----------
    *options : Option
        The option objects to include in this Options instance.
    """
    self.options = copy.deepcopy(options)

PARSED class-attribute instance-attribute

PARSED = False

from_dict classmethod

from_dict(data: dict[str, Any]) -> Options

Creates an instance of Options from a dictionary.

The dictionary should have the following structure:

{
    "duration": "30",
    "threads": 4,
}
PARAMETER DESCRIPTION
data

The dictionary representation of the options.

TYPE: dict[str, Any]

RETURNS DESCRIPTION
Options

An instance of Options with options created from the dictionary.

Examples:

>>> data = {"duration": "30s", "threads": 4}
>>> options = Options.from_dict(data)
>>> options.duration
'30s'
>>> options.threads
4
Source code in nextmv/nextmv/options.py
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "Options":
    """
    Creates an instance of `Options` from a dictionary.

    The dictionary should have the following structure:

    ```python
    {
        "duration": "30",
        "threads": 4,
    }
    ```

    Parameters
    ----------
    data : dict[str, Any]
        The dictionary representation of the options.

    Returns
    -------
    Options
        An instance of `Options` with options created from the dictionary.

    Examples
    --------
    >>> data = {"duration": "30s", "threads": 4}
    >>> options = Options.from_dict(data)
    >>> options.duration
    '30s'
    >>> options.threads
    4
    """

    options = []
    for key, value in data.items():
        opt = Option(name=key, option_type=type(value), default=value)
        options.append(opt)

    return cls(*options)

from_options_dict classmethod

from_options_dict(
    options_dict: list[dict[str, Any]],
) -> Options

Creates an instance of Options from a list of Option objects in dict form. Each entry is the dict representation of an Option.

PARAMETER DESCRIPTION
options_dict

The list of dictionaries (Option entries).

TYPE: list[dict[str, Any]]

RETURNS DESCRIPTION
Options

An instance of Options.

Examples:

>>> options_dict = [
...     {"name": "duration", "option_type": "<class 'str'>", "default": "30s"},
...     {"name": "threads", "option_type": "<class 'int'>", "default": 4}
... ]
>>> options = Options.from_options_dict(options_dict)
>>> options.duration
'30s'
>>> options.threads
4
Source code in nextmv/nextmv/options.py
@classmethod
def from_options_dict(cls, options_dict: list[dict[str, Any]]) -> "Options":
    """
    Creates an instance of `Options` from a list of `Option` objects in
    dict form. Each entry is the dict representation of an `Option`.

    Parameters
    ----------
    options_dict : list[dict[str, Any]]
        The list of dictionaries (`Option` entries).

    Returns
    -------
    Options
        An instance of `Options`.

    Examples
    --------
    >>> options_dict = [
    ...     {"name": "duration", "option_type": "<class 'str'>", "default": "30s"},
    ...     {"name": "threads", "option_type": "<class 'int'>", "default": 4}
    ... ]
    >>> options = Options.from_options_dict(options_dict)
    >>> options.duration
    '30s'
    >>> options.threads
    4
    """

    options = []
    for opt_dict in options_dict:
        opt = Option.from_dict(opt_dict)
        options.append(opt)

    return cls(*options)

from_parameters_dict classmethod

from_parameters_dict(
    parameters_dict: list[dict[str, Any]],
) -> Options

Warning

Parameter is deprecated, use Option instead. Options.from_parameters_dict -> Options.from_options_dict

Creates an instance of Options from parameters in dict form. Each entry is the dict representation of a Parameter.

PARAMETER DESCRIPTION
parameters_dict

The list of dictionaries (parameter entries).

TYPE: list[dict[str, Any]]

RETURNS DESCRIPTION
Options

An instance of Options.

Source code in nextmv/nextmv/options.py
@classmethod
def from_parameters_dict(cls, parameters_dict: list[dict[str, Any]]) -> "Options":
    """
    !!! warning

        `Parameter` is deprecated, use `Option` instead.
        `Options.from_parameters_dict` -> `Options.from_options_dict`

    Creates an instance of `Options` from parameters in dict form. Each
    entry is the dict representation of a `Parameter`.

    Parameters
    ----------
    parameters_dict : list[dict[str, Any]]
        The list of dictionaries (parameter entries).

    Returns
    -------
    Options
        An instance of `Options`.
    """

    deprecated(
        name="Options.from_parameters_dict",
        reason="`Parameter` is deprecated, use `Option` instead. "
        "Options.from_parameters_dict -> Options.from_options_dict",
    )

    parameters = []
    for parameter_dict in parameters_dict:
        parameter = Parameter.from_dict(parameter_dict)
        parameters.append(parameter)

    return cls(*parameters)

merge

merge(*new: Options, skip_parse: bool = False) -> Options

Merges the current options with the new options.

This method cannot be used if any of the options have been parsed. When options are parsed, values are read from the command-line arguments, environment variables and default values. Merging options after parsing would result in unpredictable behavior.

PARAMETER DESCRIPTION
new

The new options to merge with the current options. At least one new option set is required to merge. Multiple Options instances can be passed.

TYPE: Options DEFAULT: ()

skip_parse

If True, the merged options will not be parsed after merging. This is useful if you want to merge further options after this merge. The default is False.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Options

The merged options object (self).

RAISES DESCRIPTION
RuntimeError

If the current options have already been parsed.

RuntimeError

If the new options have already been parsed.

Examples:

>>> opt1 = Options(Option("duration", str, "30s"))
>>> opt2 = Options(Option("threads", int, 4))
>>> opt3 = Options(Option("verbose", bool, False))
>>> merged = opt1.merge(opt2, opt3)
>>> merged.duration
'30s'
>>> merged.threads
4
>>> merged.verbose
False
Source code in nextmv/nextmv/options.py
def merge(self, *new: "Options", skip_parse: bool = False) -> "Options":
    """
    Merges the current options with the new options.

    This method cannot be used if any of the options have been parsed. When
    options are parsed, values are read from the command-line arguments,
    environment variables and default values. Merging options after parsing
    would result in unpredictable behavior.

    Parameters
    ----------
    new : Options
        The new options to merge with the current options. At least one new option set
        is required to merge. Multiple `Options` instances can be passed.
    skip_parse : bool, optional
        If True, the merged options will not be parsed after merging. This is useful
        if you want to merge further options after this merge. The default is False.

    Returns
    -------
    Options
        The merged options object (self).

    Raises
    ------
    RuntimeError
        If the current options have already been parsed.
    RuntimeError
        If the new options have already been parsed.

    Examples
    --------
    >>> opt1 = Options(Option("duration", str, "30s"))
    >>> opt2 = Options(Option("threads", int, 4))
    >>> opt3 = Options(Option("verbose", bool, False))
    >>> merged = opt1.merge(opt2, opt3)
    >>> merged.duration
    '30s'
    >>> merged.threads
    4
    >>> merged.verbose
    False
    """

    if self.PARSED:
        raise RuntimeError(
            "base options have already been parsed, cannot merge. See `Options.parse()` for more information."
        )

    if not new:
        raise ValueError("at least one new Options instance is required to merge")

    for i, opt in enumerate(new):
        if not isinstance(opt, Options):
            raise TypeError(f"expected an <Options> object, but got {type(opt)} in index {i}")
        if opt.PARSED:
            raise RuntimeError(
                f"new options at index {i} have already been parsed, cannot merge. "
                + "See `Options.parse()` for more information."
            )

    # Add the new options to the current options.
    for n in new:
        self.options += n.options

    if not skip_parse:
        self.parse()

    return self

options instance-attribute

options = deepcopy(options)

options_dict

options_dict() -> list[dict[str, Any]]

Converts the Options to a list of dicts. Each dict is the dict representation of an Option.

RETURNS DESCRIPTION
list[dict[str, Any]]

The list of dictionaries (Option entries).

Examples:

>>> options = Options(Option("duration", str, "30s"), Option("threads", int, 4))
>>> opt_dicts = options.options_dict()
>>> opt_dicts[0]["name"]
'duration'
>>> opt_dicts[1]["name"]
'threads'
Source code in nextmv/nextmv/options.py
def options_dict(self) -> list[dict[str, Any]]:
    """
    Converts the `Options` to a list of dicts. Each dict is the dict
    representation of an `Option`.

    Returns
    -------
    list[dict[str, Any]]
        The list of dictionaries (`Option` entries).

    Examples
    --------
    >>> options = Options(Option("duration", str, "30s"), Option("threads", int, 4))
    >>> opt_dicts = options.options_dict()
    >>> opt_dicts[0]["name"]
    'duration'
    >>> opt_dicts[1]["name"]
    'threads'
    """

    return [opt.to_dict() for opt in self.options]

parameters_dict

parameters_dict() -> list[dict[str, Any]]

Warning

Parameter is deprecated, use Option instead. Options.parameters_dict -> Options.options_dict

Converts the options to a list of dicts. Each dict is the dict representation of a Parameter.

RETURNS DESCRIPTION
list[dict[str, Any]]

The list of dictionaries (parameter entries).

Source code in nextmv/nextmv/options.py
def parameters_dict(self) -> list[dict[str, Any]]:
    """
    !!! warning
        `Parameter` is deprecated, use `Option` instead. `Options.parameters_dict` -> `Options.options_dict`

    Converts the options to a list of dicts. Each dict is the dict
    representation of a `Parameter`.

    Returns
    -------
    list[dict[str, Any]]
        The list of dictionaries (parameter entries).
    """

    deprecated(
        name="Options.parameters_dict",
        reason="`Parameter` is deprecated, use `Option` instead. Options.parameters_dict -> Options.options_dict",
    )

    return [param.to_dict() for param in self.options]

parse

parse()

Parses the options using command-line arguments, environment variables and default values, in that order. Under the hood, the argparse library is used. When command-line arguments are parsed, the help menu is created, thus parsing Options more than once may result in unexpected behavior.

This method is called automatically when an attribute is accessed. If you want to parse the options manually, you can call this method.

After Options have been parsed, they cannot be merged with other Options. If you need to merge Options, do so before parsing them.

Examples:

>>> import nextmv
>>>
>>> options = nextmv.Options(
...     nextmv.Option("duration", str, "30s", description="solver duration", required=False),
...     nextmv.Option("threads", int, 4, description="computer threads", required=False),
... )
>>> options.parse() # Does not raise an exception.
>>> import nextmv
>>>
>>> options = nextmv.Options(
...     nextmv.Option("duration", str, "30s", description="solver duration", required=False),
...     nextmv.Option("threads", int, 4, description="computer threads", required=False),
... )
>>> print(options.duration) # Parses the options.
>>> options.parse() # Raises an exception because the options have already been parsed.
RAISES DESCRIPTION
RuntimeError

If the options have already been parsed.

ValueError

If a required option is not provided through a command-line argument, an environment variable or a default value.

TypeError

If an option is not an Option or Parameter (deprecated) object.

ValueError

If an environment variable is not of the type of the corresponding parameter.

Source code in nextmv/nextmv/options.py
def parse(self):
    """
    Parses the options using command-line arguments, environment variables
    and default values, in that order. Under the hood, the `argparse`
    library is used. When command-line arguments are parsed, the help menu
    is created, thus parsing Options more than once may result in
    unexpected behavior.

    This method is called automatically when an attribute is accessed. If
    you want to parse the options manually, you can call this method.

    After Options have been parsed, they cannot be merged with other
    Options. If you need to merge Options, do so before parsing them.

    Examples
    -------
    >>> import nextmv
    >>>
    >>> options = nextmv.Options(
    ...     nextmv.Option("duration", str, "30s", description="solver duration", required=False),
    ...     nextmv.Option("threads", int, 4, description="computer threads", required=False),
    ... )
    >>> options.parse() # Does not raise an exception.

    >>> import nextmv
    >>>
    >>> options = nextmv.Options(
    ...     nextmv.Option("duration", str, "30s", description="solver duration", required=False),
    ...     nextmv.Option("threads", int, 4, description="computer threads", required=False),
    ... )
    >>> print(options.duration) # Parses the options.
    >>> options.parse() # Raises an exception because the options have already been parsed.

    Raises
    ------
    RuntimeError
        If the options have already been parsed.
    ValueError
        If a required option is not provided through a command-line
        argument, an environment variable or a default value.
    TypeError
        If an option is not an `Option` or `Parameter` (deprecated) object.
    ValueError
        If an environment variable is not of the type of the corresponding
        parameter.
    """

    if self.PARSED:
        raise RuntimeError("options have already been parsed")

    self._parse()

to_dict

to_dict() -> dict[str, Any]

Converts the options to a dict. As a side effect, this method parses the options if they have not been parsed yet. See the parse method for more information.

RETURNS DESCRIPTION
dict[str, Any]

The options as a dict where keys are option names and values are the corresponding option values.

Examples:

>>> options = Options(Option("duration", str, "30s"), Option("threads", int, 4))
>>> options_dict = options.to_dict()
>>> options_dict["duration"]
'30s'
>>> options_dict["threads"]
4
Source code in nextmv/nextmv/options.py
def to_dict(self) -> dict[str, Any]:
    """
    Converts the options to a dict. As a side effect, this method parses
    the options if they have not been parsed yet. See the `parse` method
    for more information.

    Returns
    -------
    dict[str, Any]
        The options as a dict where keys are option names and values
        are the corresponding option values.

    Examples
    --------
    >>> options = Options(Option("duration", str, "30s"), Option("threads", int, 4))
    >>> options_dict = options.to_dict()
    >>> options_dict["duration"]
    '30s'
    >>> options_dict["threads"]
    4
    """

    if not self.PARSED:
        self._parse()

    class model(BaseModel):
        config: dict[str, Any]

    self_dict = copy.deepcopy(self.__dict__)

    rm_keys = ["PARSED", "options"]
    for key in rm_keys:
        if key in self_dict:
            self_dict.pop(key)

    m = model.from_dict(data={"config": self_dict})

    return m.to_dict()["config"]

to_dict_cloud

to_dict_cloud() -> dict[str, str]

Converts the options to a dict that can be used in the Nextmv Cloud.

Cloud has a hard requirement that options are passed as strings. This method converts the options to a dict with string values. This is useful for passing options to the Nextmv Cloud.

As a side effect, this method parses the options if they have not been parsed yet. See the parse method for more information.

RETURNS DESCRIPTION
dict[str, str]

The options as a dict with string values where non-string values are JSON-encoded.

Examples:

>>> options = Options(Option("duration", str, "30s"), Option("threads", int, 4))
>>> cloud_dict = options.to_dict_cloud()
>>> cloud_dict["duration"]
'30s'
>>> cloud_dict["threads"]
'4'
Source code in nextmv/nextmv/options.py
def to_dict_cloud(self) -> dict[str, str]:
    """
    Converts the options to a dict that can be used in the Nextmv Cloud.

    Cloud has a hard requirement that options are passed as strings. This
    method converts the options to a dict with string values. This is
    useful for passing options to the Nextmv Cloud.

    As a side effect, this method parses the options if they have not been
    parsed yet. See the `parse` method for more information.

    Returns
    -------
    dict[str, str]
        The options as a dict with string values where non-string values
        are JSON-encoded.

    Examples
    --------
    >>> options = Options(Option("duration", str, "30s"), Option("threads", int, 4))
    >>> cloud_dict = options.to_dict_cloud()
    >>> cloud_dict["duration"]
    '30s'
    >>> cloud_dict["threads"]
    '4'
    """

    options_dict = self.to_dict()

    cloud_dict = {}
    for k, v in options_dict.items():
        if isinstance(v, str):
            cloud_dict[k] = v
        else:
            cloud_dict[k] = json.dumps(v)

    return cloud_dict

Parameter dataclass

Parameter(
    name: str,
    param_type: type,
    default: Optional[Any] = None,
    description: Optional[str] = None,
    required: bool = False,
    choices: list[Optional[Any]] = None,
)

Warning

Parameter is deprecated, use Option instead.

Parameter that is used in a Configuration. When a parameter is required, it is a good practice to provide a default value for it. This is because the configuration will raise an error if a required parameter is not provided through a command-line argument, an environment variable or a default value.

PARAMETER DESCRIPTION

name

The name of the parameter.

TYPE: str

param_type

The type of the parameter.

TYPE: type

default

The default value of the parameter. Even though this is optional, it is recommended to provide a default value for all parameters.

TYPE: Any DEFAULT: None

description

An optional description of the parameter. This is useful for generating help messages for the configuration.

TYPE: str DEFAULT: None

required

Whether the parameter is required. If a parameter is required, it will be an error to not provide a value for it, either through a command-line argument, an environment variable or a default value.

TYPE: bool DEFAULT: False

choices

Limits values to a specific set of choices.

TYPE: list[Optional[Any]] DEFAULT: None

Examples:

>>> from nextmv.options import Parameter
>>> parameter = Parameter("timeout", int, 60, "The maximum timeout in seconds", required=True)

choices class-attribute instance-attribute

choices: list[Optional[Any]] = None

Limits values to a specific set of choices.

default class-attribute instance-attribute

default: Optional[Any] = None

The default value of the parameter. Even though this is optional, it is recommended to provide a default value for all parameters.

description class-attribute instance-attribute

description: Optional[str] = None

An optional description of the parameter. This is useful for generating help messages for the configuration.

from_dict classmethod

from_dict(data: dict[str, Any]) -> Parameter

Warning

Parameter is deprecated, use Option instead. Parameter.from_dict -> Option.from_dict

Creates an instance of Parameter from a dictionary.

PARAMETER DESCRIPTION
data

The dictionary representation of a parameter.

TYPE: dict[str, Any]

RETURNS DESCRIPTION
Parameter

An instance of Parameter.

Source code in nextmv/nextmv/options.py
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "Parameter":
    """
    !!! warning
        `Parameter` is deprecated, use `Option` instead.
        `Parameter.from_dict` -> `Option.from_dict`

    Creates an instance of `Parameter` from a dictionary.

    Parameters
    ----------
    data : dict[str, Any]
        The dictionary representation of a parameter.

    Returns
    -------
    Parameter
        An instance of `Parameter`.
    """

    deprecated(
        name="Parameter.from_dict",
        reason="`Parameter` is deprecated, use `Option` instead. Parameter.from_dict -> Option.from_dict",
    )

    param_type_string = data["param_type"]
    param_type = getattr(builtins, param_type_string.split("'")[1])

    return Parameter(
        name=data["name"],
        param_type=param_type,
        default=data.get("default"),
        description=data.get("description"),
        required=data.get("required", False),
        choices=data.get("choices"),
    )

name instance-attribute

name: str

The name of the parameter.

param_type instance-attribute

param_type: type

The type of the parameter.

required class-attribute instance-attribute

required: bool = False

Whether the parameter is required. If a parameter is required, it will be an error to not provide a value for it, either trough a command-line argument, an environment variable or a default value.

to_dict

to_dict() -> dict[str, Any]

Warning

Parameter is deprecated, use Option instead. Parameter.to_dict -> Option.to_dict

Converts the parameter to a dict.

RETURNS DESCRIPTION
dict[str, Any]

The parameter as a dict with its name, type, default value, description, required flag, and choices.

Examples:

>>> param = Parameter("timeout", int, 60, "Maximum time in seconds", True)
>>> param_dict = param.to_dict()
>>> param_dict["name"]
'timeout'
>>> param_dict["default"]
60
Source code in nextmv/nextmv/options.py
def to_dict(self) -> dict[str, Any]:
    """
    !!! warning
        `Parameter` is deprecated, use `Option` instead.
        `Parameter.to_dict` -> `Option.to_dict`

    Converts the parameter to a dict.

    Returns
    -------
    dict[str, Any]
        The parameter as a dict with its name, type, default value,
        description, required flag, and choices.

    Examples
    --------
    >>> param = Parameter("timeout", int, 60, "Maximum time in seconds", True)
    >>> param_dict = param.to_dict()
    >>> param_dict["name"]
    'timeout'
    >>> param_dict["default"]
    60
    """

    deprecated(
        name="Parameter.to_dict",
        reason="`Parameter` is deprecated, use `Option` instead. Parameter.to_dict -> Option.to_dict",
    )

    return {
        "name": self.name,
        "param_type": str(self.param_type),
        "default": self.default,
        "description": self.description,
        "required": self.required,
        "choices": self.choices,
    }