Skip to content

cli

Full documentation pages are generated for docstring reference only and may contain symbols imported from other modules. Imported symbols are not distinguished from locally defined symbols and will appear in any module that they are imported into. For better information on where symbols should be imported from, review the sourcecode on the github.

FoSpy._dev.testing.cli

REV_REGISTRY module-attribute

REV_REGISTRY = {v: k for k, v in (REGISTRY.items())}

TESTS module-attribute

TESTS = {
    "Validate a Synthesis File": (
        load_test,
        {"Open Results?": ("open_result", False)},
    ),
    "Map a JSON to A Synthesis and Validate": (
        map_test,
        {
            "Open Results?": ("open_result", False),
            "Change Map Name": ("map_name", "default"),
            "Add New Map?": ("make_new", False),
            "Include Missing Values?": (
                "new_missing",
                False,
            ),
            "Set New Map As Default?": (
                "new_default",
                False,
            ),
        },
    ),
}

cli_args module-attribute

cli_args = {
    "--test": "test",
    "--loop": "loop",
    "--help": "help",
    "--switch": "switch",
}

shortcuts module-attribute

shortcuts = {
    "-t": "--test",
    "-l": "--loop",
    "-h": "--help",
    "-s": "--switch",
}

_coerce_value

_coerce_value(key, value, test_name)
Source code in FoSpy/_dev/testing/cli.py
def _coerce_value(key, value, test_name):
    if not test_name or test_name not in TESTS:
        return value

    _, options = TESTS[test_name]

    if key not in options:
        return value

    arg_name, default = options[key]
    expected_type = _infer_type_from_default(default)

    # Boolean coercion
    if expected_type is bool:
        v = value.lower()
        if v in ("1", "true", "yes", "y", "on"):
            return True
        if v in ("0", "false", "no", "n", "off"):
            return False
        print(f"Invalid boolean for --{key.replace('_','-')}: {value}")
        return None

    # Numeric coercion
    if expected_type in (int, float):
        try:
            return expected_type(value)
        except ValueError:
            print(f"Invalid {expected_type.__name__} for --{key.replace('_','-')}: {value}")
            return None

    # Default: string
    return value

_get_args

_get_args()
Source code in FoSpy/_dev/testing/cli.py
def _get_args():
    argv = sys.argv[1:]

    # Expand shortcuts
    expanded = [shortcuts.get(arg, arg) for arg in argv]

    # Help check BEFORE parsing anything else
    if "--help" in expanded or "-h" in expanded:
        return "HELP", {}, False, False

    parsed = {}
    i = 0

    while i < len(expanded):
        arg = expanded[i]

        if arg not in cli_args:
            print(f"Unknown argument: {arg}")
            return "STOP", {}, False, False

        key = cli_args[arg]

        if key in ("loop", "switch"):
            parsed[key] = True
            i += 1
            continue

        if key == "help":
            return "HELP", {}, False, False

        # Flags requiring a value
        if i + 1 >= len(expanded):
            print(f"Flag {arg} requires a value.")
            return "STOP", {}, False, False

        parsed[key] = expanded[i + 1]
        i += 2

    test_name = parsed.get("test")

    args = {}
    for k, v in parsed.items():
        if k in ("test", "loop", "switch"):
            continue

        coerced = _coerce_value(k, v, test_name)
        if coerced is None:
            return "STOP", {}, False, False

        # Map internal key → actual argument name
        arg_name, _ = TESTS[test_name][1][k]
        args[arg_name] = coerced


    loop_flag = parsed.get("loop", False)
    switch_flag = parsed.get("switch", False)

    return test_name, args, loop_flag, switch_flag

_infer_type_from_default

_infer_type_from_default(default)
Source code in FoSpy/_dev/testing/cli.py
def _infer_type_from_default(default):
    typ = type(default)

    if typ in (int, float, bool):
        return typ
    return str

_main

_main(test=None, **args)
Source code in FoSpy/_dev/testing/__init__.py
def main(test=None, **args):
    from .ui import get_test, get_options

    if test is None:
        while get_test(**args):
            pass
        return False

    else:
        test = REGISTRY.get(test, None)
        if test is None:
            raise ValueError(f"Unknown test name {test}")

        for name, (t, options) in TESTS.items():
            if test == t:
                if not args:
                    args = get_options(options, name)
                    if args is None:
                        return True
                    elif not args:
                        return False

                return test.run(**args)

        raise ValueError(f"Could not find arguments for {test.__name__}")

_print_help

_print_help()
Source code in FoSpy/_dev/testing/cli.py
def _print_help():
    print("Usage: fos-dev-test [options]\n")
    print("Global options:")
    print("  --test <name>       Select a test to run")
    print("  --loop              Repeat until test returns False")
    print("  --switch            Switch between main/dev feature branches")
    print("  --help, -h          Show this help message\n")
    print("  <no options>        Open UI Loop\n")

    print("Available tests:")
    for label, (test, options) in TESTS.items():
        print(f"  {label} ('{REV_REGISTRY[test]}')")
        for label, (opt,default) in options.items():
            flag = f"--{opt.replace('_', '-')}"
            typ = _infer_type_from_default(default)
            typename = (
                "bool" if typ is bool
                else typ.__name__
            )
            print(f"    {flag} <{typename}>   (default={default}) | {label}")
        print()

main

main()
Source code in FoSpy/_dev/testing/cli.py
def main():
    test_name, args, loop_flag, switch_flag = _get_args()
    if test_name == "HELP":
        _print_help()
        return

    if test_name == "STOP":
        print("Try --help or -h for more information.")
        return

    if switch_flag:
        from ._utils import toggle_branches
        toggle_branches()
        return

    print_logo()
    if loop_flag:
        while _main(test=test_name, **args):
            pass
        return

    _main(test=test_name, **args)
print_logo()
Source code in FoSpy/_dev/testing/_utils.py
def print_logo():
    print(LOGO)
    print(PREAMBLE)