# This file was auto generated by Styx.
# Do not edit this file directly.

import typing
import pathlib
from styxdefs import *

CIFTI_LABEL_EXPORT_TABLE_METADATA = Metadata(
    id="6da54211fa44d381c5a103ea7e5e59ecf82e5e80.workbench",
    name="cifti-label-export-table",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_CiftiLabelExportTableParamsDictNoTag = typing.TypedDict('_CiftiLabelExportTableParamsDictNoTag', {
    "label-in": InputPathType,
    "map": str,
    "table-out": str,
    "json-out": typing.NotRequired[str | None],
})
CiftiLabelExportTableParamsDictTagged = typing.TypedDict('CiftiLabelExportTableParamsDictTagged', {
    "@type": typing.Literal["workbench/cifti-label-export-table"],
    "label-in": InputPathType,
    "map": str,
    "table-out": str,
    "json-out": typing.NotRequired[str | None],
})
CiftiLabelExportTableParamsDict = _CiftiLabelExportTableParamsDictNoTag | CiftiLabelExportTableParamsDictTagged


class CiftiLabelExportTableOutputs(typing.NamedTuple):
    """
    Output object returned when calling `CiftiLabelExportTableParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""


def cifti_label_export_table_params(
    label_in: InputPathType,
    map_: str,
    table_out: str,
    json_out: str | None = None,
) -> CiftiLabelExportTableParamsDictTagged:
    """
    Build parameters.
    
    Args:
        label_in: the input cifti label file.
        map_: the number or name of the label map to use.
        table_out: output - the output text file.
        json_out: export the hierarchy as json\
            \
            output - filename to write hierarchy to.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/cifti-label-export-table",
        "label-in": label_in,
        "map": map_,
        "table-out": table_out,
    }
    if json_out is not None:
        params["json-out"] = json_out
    return params


def cifti_label_export_table_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `CiftiLabelExportTableParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("label-in", None) is None:
        raise StyxValidationError("`label-in` must not be None")
    if not isinstance(params["label-in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`label-in` has the wrong type: Received `{type(params.get("label-in", None))}` expected `InputPathType`')
    if params.get("map", None) is None:
        raise StyxValidationError("`map` must not be None")
    if not isinstance(params["map"], str):
        raise StyxValidationError(f'`map` has the wrong type: Received `{type(params.get("map", None))}` expected `str`')
    if params.get("table-out", None) is None:
        raise StyxValidationError("`table-out` must not be None")
    if not isinstance(params["table-out"], str):
        raise StyxValidationError(f'`table-out` has the wrong type: Received `{type(params.get("table-out", None))}` expected `str`')
    if params.get("json-out", None) is not None:
        if not isinstance(params["json-out"], str):
            raise StyxValidationError(f'`json-out` has the wrong type: Received `{type(params.get("json-out", None))}` expected `str | None`')


def cifti_label_export_table_cargs(
    params: CiftiLabelExportTableParamsDict,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.extend([
        "wb_command",
        "-cifti-label-export-table"
    ])
    cargs.append(execution.input_file(params.get("label-in", None)))
    cargs.append(params.get("map", None))
    cargs.append(params.get("table-out", None))
    if params.get("json-out", None) is not None:
        cargs.extend([
            "-hierarchy",
            params.get("json-out", None)
        ])
    return cargs


def cifti_label_export_table_outputs(
    params: CiftiLabelExportTableParamsDict,
    execution: Execution,
) -> CiftiLabelExportTableOutputs:
    """
    Build outputs object containing output file paths and possibly stdout/stderr.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Outputs object.
    """
    ret = CiftiLabelExportTableOutputs(
        root=execution.output_file("."),
    )
    return ret


def cifti_label_export_table_execute(
    params: CiftiLabelExportTableParamsDict,
    runner: Runner | None = None,
) -> CiftiLabelExportTableOutputs:
    """
    EXPORT LABEL TABLE FROM CIFTI AS TEXT.
    
    Takes the label table from the cifti label map, and writes it to a text
    format matching what is expected by -cifti-label-import.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiLabelExportTableOutputs`).
    """
    cifti_label_export_table_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(CIFTI_LABEL_EXPORT_TABLE_METADATA)
    params = execution.params(params)
    cargs = cifti_label_export_table_cargs(params, execution)
    ret = cifti_label_export_table_outputs(params, execution)
    execution.run(cargs)
    return ret


def cifti_label_export_table(
    label_in: InputPathType,
    map_: str,
    table_out: str,
    json_out: str | None = None,
    runner: Runner | None = None,
) -> CiftiLabelExportTableOutputs:
    """
    EXPORT LABEL TABLE FROM CIFTI AS TEXT.
    
    Takes the label table from the cifti label map, and writes it to a text
    format matching what is expected by -cifti-label-import.
    
    Args:
        label_in: the input cifti label file.
        map_: the number or name of the label map to use.
        table_out: output - the output text file.
        json_out: export the hierarchy as json\
            \
            output - filename to write hierarchy to.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `CiftiLabelExportTableOutputs`).
    """
    params = cifti_label_export_table_params(
        label_in=label_in,
        map_=map_,
        table_out=table_out,
        json_out=json_out,
    )
    return cifti_label_export_table_execute(params, runner)


__all__ = [
    "CIFTI_LABEL_EXPORT_TABLE_METADATA",
    "CiftiLabelExportTableOutputs",
    "CiftiLabelExportTableParamsDict",
    "CiftiLabelExportTableParamsDictTagged",
    "cifti_label_export_table",
    "cifti_label_export_table_execute",
    "cifti_label_export_table_params",
]
