Coverage for src / rendercv / cli / render_command / parse_override_arguments.py: 100%
16 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 13:49 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 13:49 +0000
1import typer
3from rendercv.exception import RenderCVUserError
6def parse_override_arguments(extra_arguments: typer.Context) -> dict[str, str]:
7 """Parse CLI override arguments into dotted-path dictionary.
9 Why:
10 Users need quick field overrides without editing YAML. We need to parse
11 `--cv.phone "123"` style arguments.
13 Example:
14 ```py
15 # From: rendercv render cv.yaml --cv.name "Jane" --cv.phone "456"
16 args = parse_override_arguments(ctx)
17 # Returns: {"cv.name": "Jane", "cv.phone": "456"}
18 ```
20 Args:
21 extra_arguments: Typer context containing unparsed arguments.
23 Returns:
24 Map of dotted paths to override values.
25 """
26 key_and_values: dict[str, str] = {}
28 # `extra_arguments.args` is a list of arbitrary arguments that haven't been
29 # specified in `cli_render_command` function's definition. They are used to allow
30 # users to edit their data model in CLI. The elements with even indexes in this list
31 # are keys that start with double dashed, such as
32 # `--cv.sections.education.0.institution`. The following elements are the
33 # corresponding values of the key, such as `"Bogazici University"`. The for loop
34 # below parses `ctx.args` accordingly.
36 if len(extra_arguments.args) % 2 != 0:
37 message = (
38 "There is a problem with the extra arguments"
39 f" ({','.join(extra_arguments.args)})! Each key should have a corresponding"
40 " value."
41 )
42 raise RenderCVUserError(message)
44 for i in range(0, len(extra_arguments.args), 2):
45 key = extra_arguments.args[i]
46 value = extra_arguments.args[i + 1]
47 if not key.startswith("--"):
48 message = f"The key ({key}) should start with double dashes!"
49 raise RenderCVUserError(message)
51 key = key.replace("--", "")
53 key_and_values[key] = value
55 return key_and_values