Skip to content

confit.cli

Cli [source]

Bases: Typer

Custom Typer object that:

  • validates a command parameters before executing it
  • accepts a configuration file describing the parameters
  • automatically instantiates parameters given a dictionary when type hinted

parse_overrides [source]

Parse the overrides from the command line into a dictionary of key-value pairs.

Parameters

PARAMETER DESCRIPTION
args

The arguments to parse

TYPE: List[str]

RETURNS DESCRIPTION
Dict[str, Any]

The parsed overrides as a dictionary

Source code in confit/cli.py
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
49
50
51
def parse_overrides(args: List[str]) -> Dict[str, Any]:
    """
    Parse the overrides from the command line into a dictionary
    of key-value pairs.

    Parameters
    ----------
    args: List[str]
        The arguments to parse

    Returns
    -------
    Dict[str, Any]
        The parsed overrides as a dictionary
    """
    result = {}
    while args:
        opt = args.pop(0)
        err = f"Invalid config override '{opt}'"
        if opt.startswith("--"):  # new argument
            opt = opt.replace("--", "")
            if "=" in opt:  # we have --opt=value
                opt, value = opt.split("=", 1)
            else:
                if not args or args[0].startswith("--"):  # flag with no value
                    value = "true"
                else:
                    value = args.pop(0)
            opt = opt.replace("-", "_")
            result[opt] = loads(value)
        else:
            secho(f"{err}: doesn't support shorthands", fg=colors.RED)
            exit(1)
    return result