Skip to content

CLI Utilities

Command-line interface utilities for running executors.


get_cli_parser

get_cli_parser()

Build an argument parser for the executor CLI.

Returns:

Type Description

An argparse.ArgumentParser configured with --executor,

--input, and --output-location arguments.

Source code in src/aibs_informatics_core/executors/cli.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def get_cli_parser():
    """Build an argument parser for the executor CLI.

    Returns:
        An ``argparse.ArgumentParser`` configured with ``--executor``,
        ``--input``, and ``--output-location`` arguments.
    """
    import argparse

    parser = argparse.ArgumentParser(description="Executor CLI")

    parser.add_argument(
        "--executor",
        dest="executor",
        required=True,
        help=(
            "executor class to run. Must be a fully qualified name of the executor class. "
            "e.g. aibs_informatics_aws_lambda.common.executor.BaseExecutor"
        ),
    )
    parser.add_argument(
        "--input",
        "--request",
        "-i",
        dest="request",
        required=True,
        help=(
            "input to executor. Can be a json string, json file, or S3 location. "
            "e.g. s3://bucket/key, /path/to/file.json, '{'foo': 'bar'}'"
        ),
    )
    parser.add_argument(
        "--output-location",
        "--response-location",
        "-o",
        dest="output_location",
        required=False,
        help=("optional response location to store response at. can be S3 or local file."),
    )
    return parser

run_cli_executor

run_cli_executor(args=None)

Run an executor from the command line.

Parses CLI arguments to load an executor class and execute it with the provided input. Optionally writes the response to an output location.

Parameters:

Name Type Description Default
args list[str] | None

Optional list of CLI arguments. If None, reads from sys.argv.

None

Raises:

Type Description
ValueError

If the specified executor class is not a subclass of BaseExecutor.

Source code in src/aibs_informatics_core/executors/cli.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def run_cli_executor(args: list[str] | None = None):
    """Run an executor from the command line.

    Parses CLI arguments to load an executor class and execute it with the
    provided input. Optionally writes the response to an output location.

    Args:
        args: Optional list of CLI arguments. If None, reads from ``sys.argv``.

    Raises:
        ValueError: If the specified executor class is not a subclass of ``BaseExecutor``.
    """
    parsed_args = get_cli_parser().parse_args(args=args)

    executor_class = load_type_from_qualified_name(parsed_args.executor)

    if not issubclass(executor_class, BaseExecutor):
        raise ValueError(f"Executor class {executor_class} is not a subclass of BaseExecutor")

    executor_class.run_executor(parsed_args.request, parsed_args.output_location)