Skip to content

Adapting how Lists are handled

Default

With lists we have a number of different ways on how to deal with them.

The basic version is that lists with options are used by specifying the option multiple times. For this behaviour nothing special needs to be done.

Basic version for options with List type

> python examples/lists/default.py --help
Usage: examples/lists/default.py [OPTIONS]

╭─ Eager Callbacks ────────────────────────────────────────────────────────────────────────────────╮
│   --help     Display the help message                                                            │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────╮
│   --x     int*                                                                                   │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
> python examples/lists/default.py --x 1 --x 2
x: [1, 2]
"""
Very simple example of a CLI with a list
"""
from typing import List

from thermite import Config, run


def simple(x: List[int]):
    print(f"x: {x}")


if __name__ == "__main__":
    config = Config()
    run(simple, config=config)

Passing multiple arguments for a single call to option

We can also set it up so that we can pass multiple values to the list for a single call to the option.

Single option with variable length arguments

> python examples/lists/var_length_list_opt.py --help
Usage: examples/lists/var_length_list_opt.py [OPTIONS]

╭─ Eager Callbacks ────────────────────────────────────────────────────────────────────────────────╮
│   --help     Display the help message                                                            │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────╮
│   --x     List[int]                                                                              │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
> python examples/lists/var_length_list_opt.py --x 1 2 3
x: [1, 2, 3]
"""
Very simple example of a CLI with a list
and variable number of values for the option
"""
from typing import List

from thermite import Config, Event, Option, ParameterGroup, run


def simple(x: List[int]):
    print(f"x: {x}")


def pg_multi_opt(pg: ParameterGroup) -> ParameterGroup:
    pg["x"].processors[0] = pg["x"].processors[0].to_convert_trigger_processor()
    return pg


if __name__ == "__main__":
    config = Config()
    config.event_cb_deco(Event.PG_POST_CREATE, simple)(pg_multi_opt)
    run(simple, config=config)

Multiple lists as arguments with no-op separator

in this library even multiple variable length arguments are possible, we just have to seperate them with a no-op option.

multiple lists with variable arguments

> python examples/lists/multiple_args.py --help
Usage: examples/lists/multiple_args.py ARGS

╭─ Eager Callbacks ────────────────────────────────────────────────────────────────────────────────╮
│   --0        Works as a delimiter; no other operation                                            │
│   --help     Display the help message                                                            │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Arguments ──────────────────────────────────────────────────────────────────────────────────────╮
│   x     List[int]                                                                                │
│   y     List[int]                                                                                │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
> python examples/lists/multiple_args.py 1 2 3 --0 4 5
x: [1, 2, 3]
y: [4, 5]
"""
Very simple example of a CLI with multiple lists as arguments
"""
from typing import Any, List

from thermite import CliParamKind, Config, Event, ObjSignature, run
from thermite.callbacks import noop_callback


def simple(x: List[int], y: List[int]):
    print(f"x: {x}")
    print(f"y: {y}")


def opt_to_arg(sig: ObjSignature, _: Any):
    sig.params["x"].cli_kind = CliParamKind.ARGUMENT
    sig.params["y"].cli_kind = CliParamKind.ARGUMENT

    return sig


if __name__ == "__main__":
    config = Config()
    config.event_cb_deco(Event.SIG_EXTRACT, simple)(opt_to_arg)
    config.add_cli_callback(noop_callback)
    run(simple, config=config)

Setting an empty list (if it is not the default)

We can also set a trigger that sets a parameter to an empty list.

Trigger for empty list

"""
Very simple example of a CLI with empty list
"""
from typing import List

from thermite import (
    Config,
    ConstantTriggerProcessor,
    Event,
    Option,
    ParameterGroup,
    run,
)


def simple(x: List[int], y: List[int]):
    print(f"x: {x}")
    print(f"y: {y}")


def pg_empty_list(pg: ParameterGroup):
    assert isinstance(pg.params["x"], Option)
    pg.params["x"].processors.append(
        ConstantTriggerProcessor(triggers=["--x-empty"], res_type=[], constant=[])
    )
    return pg


if __name__ == "__main__":
    config = Config()
    config.event_cb_deco(Event.PG_POST_CREATE, simple)(pg_empty_list)
    run(simple, config=config)
> python examples/lists/empty_list.py --help
Usage: examples/lists/empty_list.py [OPTIONS]

╭─ Eager Callbacks ────────────────────────────────────────────────────────────────────────────────╮
│   --help     Display the help message                                                            │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────╮
│   --x           int*                                                                             │
│   --x-empty     []                                                                               │
│   --y           int*                                                                             │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
> python examples/lists/empty_list.py --x-empty --y 4 --y 5
x: []
y: [4, 5]

Show lists with:

  • an option to specify an empty list