diff options
| -rw-r--r-- | patternutils/commands/tmv.py | 14 |
1 files changed, 13 insertions, 1 deletions
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 @@ -79,6 +79,12 @@ def move_file(old_filename: str, new_filename: str) -> None: 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", is_flag=True, @@ -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: |
