From fd05c120f5fc0db663c3a9b7a99c0b9c2216cbcb Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Mon, 14 Jul 2025 14:58:48 +0200 Subject: Add --abort-on-key-not-found in tmv --- patternutils/commands/tmv.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/patternutils/commands/tmv.py b/patternutils/commands/tmv.py index 4fda28c..150f67d 100644 --- a/patternutils/commands/tmv.py +++ b/patternutils/commands/tmv.py @@ -78,6 +78,12 @@ def move_file(old_filename: str, new_filename: str) -> None: default=False, help="Exit if a renamed path already exists.", ) +@click.option( + "--abort-on-key-not-found", + is_flag=True, + default=False, + help="Exit if the specified key (specified by the --key option) is not found in the input JSON.", +) @click.option( "-n", "--dry-run", @@ -101,6 +107,7 @@ def tmv( force: bool, template_engine: template.TemplateEngines, abort_on_path_exist: bool, + abort_on_key_not_found: bool, use_predefined_template: bool, read_from: TextIO, key: str, @@ -145,7 +152,12 @@ def tmv( click.secho("Jinja2 not installed!", fg="red", err=True) raise SystemExit(1) - new_filenames = dict((entry[key], render(**entry)) for entry in data) + try: + new_filenames = {entry[key]: render(**entry) for entry in data} + except KeyError: + click.secho(f"Key '{key}' not found in input data.", fg="red", err=True) + if abort_on_key_not_found: + raise SystemExit(1) if editor: try: -- cgit v1.3.1